Servlet interview questions

What is the use of ServletContext ?

Using ServletContext, We can access data from its environment. Servlet context is common to all Servlets so all Servlets share the information through ServeltContext.

Is there any way to generate PDF’S dynamically in servlets?

We need to use iText. A open source library for java. Please refer sourceforge site for sample servlet examples.

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

getSession(true) – This method will check whether already a session is existing for the user. If a session is existing, it will return that session object, Otherwise it will create new session object and return taht object.
getSession(false) – This method will check existence of session. If session exists, then it returns the reference of that session object, if not, this methods will return null.

Which code line must be set before any of the lines that use the PrintWriter?

setContentType() method must be set.

Which protocol will be used by browser and servlet to communicate ?


In how many ways we can track the sessions?

Method 1) By URL rewriting
Method 2) Using Session object
Getting Session form HttpServletRequest object
HttpSession session = request.getSession(true);
Get a Value from the session
Adding values to session
cart = new Cart();
session.putValue(session.getId(), cart);
At the end of the session, we can inactivate the session by using the following command
Method 3) Using cookies
Method 4) Using hidden fields

How Can You invoke other web resources (or other servlet / jsp ) ?

Servelt can invoke other Web resources in two ways: indirect and direct.
Indirect Way : Servlet will return the resultant HTML to the browser which will point to another Servlet (Web resource)
Direct Way : We can call another Web resource (Servelt / Jsp) from Servelt program itself, by using RequestDispatcher object.
You can get this object using getRequestDispatcher(“URL”) method. You can get this object from either a request or a Context.
Example :

RequestDispatcher dispatcher = request.getRequestDispatcher("/jspsample.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);

How Can you include other Resources in the Response?

Using include method of a RequestDispatcher object.
Included WebComponent (Servlet / Jsp) cannot set headers or call any method (for example, setCookie) that affects the headers of the response.
Example : RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(“/banner”);
if (dispatcher != null)
dispatcher.include(request, response);

READ  Life Cycle of Servlet

What is servlet mapping?

The servlet mapping defines an association between a URL pattern and a servlet. The mapping is used to map requests to Servlets.

Can we use the constructor, instead of init(), to initialize servlet?

Yes. But you will not get the servlet specific things from constructor. The original reason for init() was that old versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructor a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.

Can we write a constructor for servlet ?

Yes. But the container will always call the default constructor only. If default constructor is not present, the container will throw an exception.

What are the parameters for service method?

ServletRequest and ServletResponse.