Logo F2FInterview

Servlet Interview Questions

Q   |   QA

setContentType() method must be set.


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

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);

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);

In order to link this F2FInterview's page as Reference on your website or Blog, click on below text area and pres (CTRL-C) to copy the code in clipboard or right click then copy the following lines after that paste into your website or Blog.

Get Reference Link To This Page: (copy below code by (CTRL-C) and paste into your website or Blog)
HTML Rendering of above code: