Pr.Pg Next Pg

Servlet Interview Questions and Answers Part 4

 

31. What are different Authentication options available in Servlets.

  • There are four ways of Authentication options available in servlets

  • HTTP basic authentication
    In this, server uses the username and password provided by the client and these credentials are transmitted using simple base64 encoding.

  • HTTP digest authentication
    This option is same the basic authentication except the password is encrypted and transmitted using SHA or MD5.

  • HTTPS client authentication
    This options is based on HTTP over SSL.

  • Form-based authentication
    Form-based authentication uses login page to collect username and password.

 

32.Explain why HttpServlet is declared abstract.

  • The Constructor HttpServlet() does nothing because this is an abstract class.

  • Default implementations in a few Java classes like HttpServlet don’t really do anything. Hence, they need to be overridden.

  • Usually one of the following methods of HttpServlet must be overridden by a subclass:

  • doGet, if the servlet supports HTTP GET requests

  • doPost, HTTP POST requests

  • doPut, HTTP PUT requests

  • doDelete, HTTP DELETE requests

  • init and destroy, to manage resources

  • getServletInfo, to provide information

  • However, there doesn’t seem to be any reason why the service method should be overridden because it eventually dispatches the task to one of the doXXX methods.

 

33.What is the GenericServlet class?

  • GenericServlet makes writing servlets easier. To write a generic servlet, all you need to do is to override the abstract service method.

 

34.Is possible to have a constructor for a servlet?

  • Yes, it is possible to have a constructor for a servlet. However, it is not practiced usually.

  • The operations with the constructor can be performed as usual just that it cannot be called explicitly using the ‘new’ keyword.

  • This is implicitly handled by the servlet container.

  • Thus, the container performs initialization as well as constructor functions.

 

35.Is possible to have a constructor for a servlet?

  • The init() method creates and loads the servlet. However, the servlet instance creation of is handled by the servlet container which internally calls the constructor. The communication between the servlet and the servlet container is done through ServletConfig.

  • ServletConfig is a servlet configuration object. It is used by a servlet container to pass information to a servlet during initialization.

  • Servlet class cannot declare a constructor with ServletConfig object as a argument and cannot access ServletConfig object. 

 

36.What is the difference between an Applet and a Servlet
Applets

  • Applets are applications designed to be transmitted over the network and executed by Java compatible web browsers.

  • An Applet is a client side java program that runs within a Web browser on the client machine.

  • An applet can use the user interface classes like AWT or Swing.

  • Applet Life Cycle Methods: init(), stop(), paint(), start(), destroy()

Servlets

  • Servlets are Java based analog to CGI programs, implemented by means of servlet container associated with an HTTP server.

  • Servlet is a server side component which runs on the web server.

  • The servlet does not have a user interface.

  • Servlet Methods: doGet(), doPost() 

 

37.Define HTTP Tunneling?

  • In some organizations, the intranet is blocked by a firewall to the internet.

  • It is exposed to the outer networks only by means of webserver port that accept only Http requests. In such situations, if protocols other than http are used, then they get rejected.

  • The solution is to have them encapsulated in http or https and sent as an HttpRequest.

  • Thus, masking other protocols as http requests is called HTTP Tunneling.

 

38. List out the difference between ServletConfig and ServletContext?

  • Both are interfaces in the package javax.servlet.

  • ServletConfig is a servlet configuration object. It is used by a servlet container to pass information to a servlet during initialization.

  • The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets.

  • The ServletContext object is contained within the ServletConfig object. It is provided by the web server to the servlet when the servlet is initialized.

  • ServletContext is an interface which has a set of methods like getServletName(), getServletContext(), getInitParameter(), getInitParameterNames(). The servlet uses to interact with its servlet container. 

  • ServletContext is common to all servlets within the same web application. So, servlets use ServletContext to share context information.

 

39.What is the difference between difference between doGet() and doPost()?

  • doGet() and doPost() are HTTP requests handled by servlet classes.

  • In doGet(), the parameters are appended to the URL and sent along with header information. This does not happen in case of doPost(). In doPost(), the parameters are sent separately.

  • Since most of the web servers support only a limited amount of information to be attached to the headers, the size of this header should not exceed 1024 bytes. doPost() does not have this constraint.

  • Usually programmers find it difficult to choose between doGet() and doPost().

  • doGet() shall be used when small amount of data and insensitive data like a query has to be sent as a request.

  • doPost() shall be used when comparatively large amount of sensitive data has to be sent. Examples are sending data after filling up a form or sending login id and password.

  • Following example would help in understand the working of the doGet() and doPost() better:

  • When you type the word ‘java’ in google search bar and hit enter, this is what you will find in the address bar:

  • http://www.google.co.in/search?hl=en&q=java&meta=
    This is an example of doGet().

  • Had it been doPost(), you wouldn’t have been able tosee“hl=en&q=java&meta=” in the address bar.

 

40. What is the difference between using getSession(true) and getSession(false) methods?

  • getSession(true) will check whether a session already exists for the user.

  • If yes, it will return that session object else it will create a new session object and return it.

  • getSession(false) will check existence of session.

  • If session exists, then it returns the reference of that session object, if not, this methods will return null.


Pr.Pg border                                              Next Pg

Warning: include_once(bottompad.php) [function.include-once]: failed to open stream: No such file or directory in D:\INETPUB\VHOSTS\cbtsam.com\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-4.php on line 376

Warning: include_once() [function.include]: Failed opening 'bottompad.php' for inclusion (include_path='.;C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP5\pear;./includes;./pear') in D:\INETPUB\VHOSTS\cbtsam.com\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-4.php on line 376

Warning: include_once(rightpad.php) [function.include-once]: failed to open stream: No such file or directory in D:\INETPUB\VHOSTS\cbtsam.com\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-4.php on line 382

Warning: include_once() [function.include]: Failed opening 'rightpad.php' for inclusion (include_path='.;C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP5\pear;./includes;./pear') in D:\INETPUB\VHOSTS\cbtsam.com\httpdocs\servlet-tutorials\servlet-tutorials-servlets-interview-part-4.php on line 382