Skip to content
 

Servlet interview Questions with Answers part1

Below please find some Java Servlet interview questions and answers.

What is a Servlet?

A servlet is a Java program which requires a Web or application server to run. It forms a middle layer between requests coming from a Web browser or other HTTP client and databases or applications on the HTTP server. Servlet offer an efficient platform-independent replacement for CGI scripts. Servers that can host servlets are Java-enabled servers that respond to client requests.


What is the difference between GenericServlet and HttpServlet?

Generic servlet as the name suggests aims to provide the middle tier solution for non-Http based application like FTP, which means these are protocol independent and can handle multiple types of protocols. While HttpServlet are used for Http based application and can only be implemented for Hyper Text Transfer Protocols.

 

State few brief points on how Servlet technology excels over CGI scripts?

The following points show how servlet technology excels over the traditional CGI scripts: -


CGI

Servlet

1. Written in C,C++, VB and Perl which are all platform dependant.

1. Written in platform independent Java language.

2. For each request heavy weight process is created.

2. Each request is handled by light weight thread.

3. Difficult to maintain, non-scalable and non-manageable

3. Increased scalability, reusability (component based model).

4. Prone to security problems of programming language

4. Leverages built in security of Java language.


 Discuss the basic structure of a Servlet.

The basic structure of a servlet is summarized as: -

  1. It uses simple Java codes within it.
  2. The javax.servlet and the javax.servlet.http packages provides the classes and interfaces for writing servlets.
  3. It is a sub class of HttpServlet class.
  4. It generally throws ServletException and IO Exceptions.
  5. It overrides the doGet and doPost methods
  6. There are two arguments of doGet or doPost method

a.HttpServletRequest b.HttpServletResponse


  Discuss briefly about the methods called in a life cycle of a servlet

The init method – The init method is invoked when the servlet is first created and initializes the global variables and loads the classes into memory. This method is not called again.

The service Method: After creating a servlet each time the server receives a request the server creates a new thread and calls service.

The destroy Method: If the server decides to unload a servlet instance, it first call the destroy method. This method gives a chance to close database connection and other cleanup activities.


What is session tracking? State its uses.

HTTP is a state less protocol and requires some technique by which user data can be maintained persistently between pages. Session tracking helps in solving this. Session tracking can be done in following ways:-

1. Cookies – Storing a small client side data, which is used to hold that particular user specific data only.

2. Hidden data fields – Hidden fields of a page is used to store serializable objects.

3. Session state variables – Memory portion of the web server is assigned to hold data.

4. URL rewriting – URL itself can contain data like in a query string where a page’s data is attached with the URL to pass client’s specific data.

 

What is ServletContext used for?

ServletContext is used by servlets to implement the following points :-


1. Set and get context-wide (application-wide) objectvalued attributes

2. Get request dispatcher to forward to or include web component

3. Access Web context-wide initialization parameters set in the web.xml file

4. Access Web resources associated with the Web context log.

5. Access other misc. information.


 Discuss the scope of ServletContext.

  • It is shared by all servlets and JSP pages within a “web application”
  • A “web application” is a collection of servlets and content installed under a specific subset of the server’s URL namespace and possibly installed via a *.war file
  • There is one ServletContext object per “web application” per Java Virtual Machine
  •  

How to gain access to a getRequestDispatcher?

The getRequestDispatcher() method takes the requested resource’s URL as an argument. The format of this argument is a slash (“/”) followed by one or more slash-separated directory names, and ending with the name of the resource. The following are examples of valid URLs:

· /ardent/myservlet

· /ardent/tests/MyServlet.class

· /myinfo.html


What is a cookie?


It is a small amount of data sent by a servlet to the web browser.A cookie saves precious server memory and can maintain state through its unique client identification property. The browser knows where and how to save this data and later send back the same to the server on request. A cookie has a name, a single value and optional attributes and it can uniquely identify a client. The server uses cookie to extract the information about the sessions.


Discuss few disadvantages of cookies.

Since a cookie may contain user information like userid, passwords, there remains a chance of cookie hijacking where a client’s cookie is used by another to gain access to other’s account. Again privacy problem occurs when sites rely on cookies for overly sensitive data. The coders have to keep extra checking since a client may disable cookie by default and also have to think some other possible way out for which he needed that cookie.


What is the difference between ServletContext and ServletConfig?

These both are interfaces. ServletConfig interface is implemented to route configuration information to a servlet. The server sends an object which in turn implements the ServletConfig interface to the servlet’s init() method. The ServletContext interface informs about the running environment to the servlets. In order to write events to a log file a standard way is also provided.

 What is ActionMapping ?

Action mapping is a technique of forwarding request/response by specifying an action class for URL and different target view. Also the page to which the control shall be passed when a validation error will be raised can be specified too. It is also specified which form bean will correspond to action

<action-mappings>

<action path=”/a” type=myclasse.A name=”myForm”>

<forward name=”succes” path=”/success.jsp”/>

<forward name=”failure” path=”/failure.jsp”/>

<action/>

</action-mappings>



servlet interview questions and answers pdf java servlets interview questions and answers pdf

2 Comments

  1. sudhansu sekhar naya says:

    Sir my question is without writing web.xml we can write servlet program?

  2. Pranali says:

    good

Leave a Reply