Servlet and JSP Interview Questions

Question 1:

What is the difference between servletConfig and servletContext?

Servlet Config

Servlet Context

1.ServletConfig object is one per servlet class. 1.ServletContext object is global to entire web application.
2.Object of ServletConfig will be created during initialization process of the servlet. 2.Object of ServletContext will be created at the time of web application deployment.
3.In web.xml – <init-param> tag is used to pass configuration data under <servlet-class> tag. 3.In web.xml – <context-param tag is used to pass configuration data under         <web-app> tag.
4.As long as a servlet is executing, ServletConfig object will be available, it will be destroyed once the servlet execution is completed. 5.As long as web application is executing, ServletContext object will be available, and it will be destroyed once the application is removed from the server.
Question 2:

Describe implicit objects in jsp.

Object Description
Request The request object provides methods to get HTTP header information such as  form data, cookies, HTTP methods etc.
response The response object  is used to add cookies , HTTP status codes etc
out JspWriter object is used to send content in a response.
session The session object is used to track session between client requests.
application This object is an instance of servlet context.
Config This object is an instance of servlet config.
pageContext pageContext object stores references to the request and response objects for each request.
page   This object is an actual reference to the instance of the page.
Exception The exception object is a wrapper containing the exception thrown from the previous page. It is typically used to generate an appropriate response to the error condition.
Question 3:

What is the difference between Web server and Application server?

Web server is designed to serve HTTP content whereas Application server is not limited to HTTP response but also can serve other request.
Application server supports distributed transaction and EJB whereas web server only supports servlet and JSP.
Application server can contain web server in them. For example :- JBoss, WAS
Application server provides following features over web server :-

  • Connection pooling
  • Transaction management
  • Messaging
  • Clustering
  • Load Balancing
  • Persistence

To read more visit Tomcat-web server or application-server

Question 4:

What are different methods of session management in servlets?

As HTTP and Web Server both are stateless, the state of between client and server is managed using additional mechanism which are as:-

  • Hidden Field
  • Cookies
  • URL Rewriting
  • SSL Session
Question 5:

What is the difference between include directive and include action?

Include directive Include action 
 Resource included by include directive is loaded during jsp translation time.  Resource included by include action is loaded during request/run time.
 Any change on included resource will not be visible in case of include directive until jsp file compiles again.  While in case of include action any change in included resource will be visible in next request.
 Syntax of Include directive: <%@ include file="file_name" %>   Syntax of Include action: <jsp:include page="file_name" />
 When using include action tag we can also pass the parameters to the included page by using param action tag  In case of include directive it is not possible.

Question 6:

When we use doGet() method & when we use doPost() method in our servlet application?

doGet() method is used when we want to intercept on HTTP GET request. This is actually used to preprocess a request like getting the data from dtatbase and displaying it on html page.
Whereas doPost() method is used when we want to intercept HTTP POST request. this is actually used to postprocess the submitted HTML form and displaying/getting the result.

Moreover HTTP GET method sends the parameter/value with URL whereas POST method sends the parameter with headers.

Question 7:

What is the difference between GenericServlet and HttpServlet?



public abstract class GenericServlet extends java.lang.Object implements Servlet, ServletConfig,

  • Generic Servlet is protocol independent.It handles all types  of protocol  like http, smtp, ftp etc.
  • GenericServlet gives a blueprint and makes writing servlet easier.
  • GenericServlet provides simple versions of the life-cycle methods init and destroy and of the methods in the ServletConfig interface.
  • GenericServlet implements the log method, declared in the ServletContext interface.
  • To write a generic servlet, it is sufficient to override the abstract service() method.



public abstract class HttpServlet extends GenericServlet implements

  • HttpServlet defines a HTTP protocol specific servlet.
  • HttpServlet gives a blueprint for Http servlet and makes writing them easier.
  • HttpServlet extends the GenericServlet and hence inherits the properties GenericServlet.
Question 8:

What is the difference between ServletContext and PageContext?

The ServletContext gives information about the container in which the servlet (or JSP) is running in. Parameters for this can be setup in the web application deployment descriptor and there is one ServletContext per web application.

The PageContext gives the servlet (or JSP) information about the request that it is currently handling and contains information about the request and any parameters, the session, the response object, a reference to the output stream and also a reference to the web application's ServletContext.

Question 9:

What is difference between forward and sendRedirect()?

sendRedirect ():  

This method is declared in HttpServletResponse Interface.

This method is used to redirect clienty request to another location for further processing that may be available on different server or different context. This transfer of control task is delegated to the browser by the container. That is, the redirect sends a header back to the browser / client. This header contains the resource url to be redirected by the browser. Then the browser initiates a new request to the given url. Since it is a new request, the old request and response object is lost.


This method is declared in RequestDispatcher Interface.

The method is used to pass the request to another resource within the same server.This process is taken care by web container when we call forward method request is sent to another resource without the client being informed. This is also called server side redirect.

Question 10:

What is difference between page and pagecontext?

Page and Pagecontext are implicit objects in jsp. These are created at JSP translated time. The page object represents the generated servlet instance itself and is used as a scope with in one jsp. Pagecontext is used to initilize all implicit objects for example :- page attributes, access to the request, response and session objects, as well as the JspWriter referenced by out.

Share interview questions


comments powered by Disqus


Social Media