Servlet Interview Questions
Servlet Interview Questions
Servlet Interview Question for Freshers
Why a Servlet class should be a public ?
A web container internally calls newInstance() method for creating an object of a Servlet class newInstance() method first checks whether a Servlet class is public or not if public then create an object otherwise it does not create an object. Because of the above reason a Servlet class must be public..
What is container ?
Container provides runtime environment for Java2ee (j2ee) applications. A web container is a predefined application provided by a server, its takes care of Servlet and JSP. Read more.......
Explain work of container.
- Life Cycle Management
- Communication Support
- Multithreaded support
- Security etc.
Difference between GenericServlet and HttpServlet
|1||The GenericServlet is an abstract class that is extended by HttpServlet to provide HTTP protocol-specific methods.||An abstract class that simplifies writing HTTP servlets. It extends the GenericServlet base class and provides an framework for handling the HTTP protocol.|
|2||The GenericServlet does not include protocol-specific methods for handling request parameters, cookies, sessions and setting response headers.||The HttpServlet subclass passes generic service method requests to the relevant doGet() or doPost() method.|
|3||GenericServlet is not specific to any protocol.||HttpServlet only supports HTTP and HTTPS protocol.|
How can the session in Servlet can be destroyed?
An existing session can be destroyed in the following two ways:
- Programatically : Using session.invalidate() method, which makes the container abandon the session on which the method is called.
- When the server itself is shutdown.
What is servlet lazy loading?
A container does not initialize the Servlet as soon as it starts up, it initializes a Servlet when it receives a request for that Servlet first time. This is called lazy loading.
What is servlet early loading?
The Servlet specification defines the <load-on-startup> element, which can be specified in the deployment descriptor (web.xml) to make the Servlet container load and initialize the Servlet as soon as it starts up.
The process of loading a Servlet before any request comes in is called early loading or preloading or preinitializing a Servlet
We can change the directory or extension name of web.xml ?
No, we can not change the directory or extension name of this web.xml because it is standard name to recognized by container at run-time.
|1||Forward action is within the same web application.||SendRedirect redirect to another web resources which are in the same web application or other web application.|
|2||Forward uses some request and response object.||SendRedirect use new request and response object.|
Why need Session Tracking ?
- Http protocol is stateless, to make stateful between client and server we need Session Tracking.
- Session Tracking is useful for online shopping, mailing application, E-Commerce application to track the conversion.
Limitation of Cookies
- It will not work if cookie is disabled from the browser.
- Cookies are text files, It does not provides security. Any one can change this file.
- With cookies need client support that means if client disable the cookies then it does not store the client location.
Why Http is design as stateless protocol ?
If Http is stateful protocol for multiple requests given by client to web application single connection will be used between browser and web server across the multiple requests. This may make clients to engage connection with web server for long time event though the connection are ideal. Due to this the web server reach to maximum connections even though most of its connection are idle. To overcome this problem Http is given as stateless.
Why doxxx() methods are empty method in Servlet ?
The main reason doxxx() methods are empty is to reduce the burden on Servlet programmer, If HttpServlet developer provided seven doxxx() methods as abstract then whenever we extending the class from HttpServlet, we need to override all seven methods are mandatory, it increase the burden on Servlet Programmer.
Early loading ?
A container doesnot initialize the servlets ass soon as it starts up, it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading.
Lazy loading ?
The servlet specification defines the element, which can be specified in the deployment descriptor (web.xml) to make the servlet container load and initialize the servlet as soon as it starts up.
The process of loading a servlet before any request comes in is called early loading or preloading or preinitializing a servlet
Difference between forward and redirect
|In forwarding, the destination resource must be java enabled resource only.||In redirection, the destination resource can be either java or non-java resource also.|
|In forwarding, both source and destination resource must run within the same server. It is not possible to communicate across the server.||In redirection, it is possible to communicate, either within the serve or even across the server.|
|In forwarding, both the data and control are forwarded to destination (by default).||In redirection, only control is redirected, but not the data (by default).|