Best Servlet Interview Questions »

What is the servlet?

Servlet is a script, which resides and executes on server side, to create dynamic HTML. In servlet programming we will use java language. A servlet can handle multiple requests concurrently

What is the architechture of servlet package?

Servlet Interface is the central abstraction. All servlets implements this Servlet Interface either directly or indirectly (may implement or extend Servlet Interfaces sub classes or sub interfaces)

Servlet

Generic Servlet

HttpServlet ( Class ) — we will extend this class to handle GET / PUT HTTP requests

MyServlet

What is the difference between HttpServlet and GenericServlet?

A GenericServlet has a service() method to handle requests.HttpServlet extends GenericServlet added new methods
doGet()
doPost()
doHead()
doPut()
doOptions()
doDelete()
doTrace() methods
Both these classes are abstract.

What’s the difference between servlets and applets?

Servlets executes on Servers. Applets executes on browser. Unlike applets, however, servlets have no graphical user interface.

What are the uses of Servlets?

A servlet can handle multiple requests concurrently, and can synchronize requests. Servlets can forward requests to other servers and servlets. Thus servlets can be used to balance load among several servers.

When doGET() method will going to execute?

When we specified method=’GET’ in HTML
Example : < name="'SSS'" method="'GET'">

When doPOST() method will going to execute?

When we specified method=’POST’ in HTML
< name="'SSS'" method="'POST'">

What is the difference between Difference between doGet() and doPost()?

GET Method : Using get method we can able to pass 2K data from HTML
All data we are passing to Server will be displayed in URL (request string).

POST Method : In this method we does not have any size limitation.
All data passed to server will be hidden, User cannot able to see this info
on the browser.

What is the servlet life cycle?

When first request came in for the servlet , Server will invoke init() method of the servlet. There after if any user request the servlet program, Server will directly executes the service() method. When Server want to remove the servlet from pool, then it will execute the destroy() method

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?

HTTP

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
session.getValue(session.getId());

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
session.invalidate();

Method 3) Using cookies

Method 4) Using hidden fields

How Can You invoke other web resources (or other servelt / 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);
}

What is the difference between the getRequestDispatcher(String path) ServletRequest interface and ServletContext interface?

The getRequestDispatcher(String path) method of ServletRequest interface accepts parameter the path to the resource to be included or forwarded to, which can be relative to the request of the calling servlet. If the path begins with a “/” it is interpreted as relative to the current context root.

The getRequestDispatcher(String path) method of ServletContext interface cannot accepts relative paths. All path must sart with a “/” and are interpreted as relative to curent context root. If the resource is not available, or if the server has not implemented a RequestDispatcher object for that type of resource, getRequestDispatcher will return null. Your servlet should be prepared to deal with this condition.

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.

2 Response to "Best Servlet Interview Questions »"

  1. Anonymous says:

    Very good collection of questions...
    send me some more if u have....
    n thank u
    liyaqath_427@yahoo.com

    Unknown says:

    HI, Friends this very usefull java servlet interview questions
    for excellent knowledge on java servlet visit http://www.itsoftpoint.com/?page_id=2425

Post a Comment

Powered by Blogger