Select Category 
 

Servlet Interview Questions Answers

Servlet Interview Question - 1 : -

What mechanisms are used by a Servlet Container to maintain session information?

Servlet Interview Answer - 1 : -

Cookies, URL rewriting, and HTTPS protocol information are used to maintain session information
 

Servlet Interview Question - 2 : -

Why is Servlet so popular?

Servlet Interview Answer - 2 : -

Because servlets are platform-independent Java classes that are compiled to platform-neutral byte code that can be loaded dynamically into and run by a Java technology-enabled Web server.
 

Servlet Interview Question - 3 : -

When a client request is sent to the servlet container, how does the container choose which servlet to invoke?

Servlet Interview Answer - 3 : -

The servlet container determines which servlet to invoke based on the configuration of its servlets, and calls it with objects representing the request and response.
 

Servlet Interview Question - 4 : -

Why is it that we can't give relative URL's when using ServletContext.getRequestDispatcher() when we can use the same while calling ServletRequest.getRequestDispatcher()?

Servlet Interview Answer - 4 : -

Since ServletRequest has the current request path to evaluae the relative path while ServletContext does not.
 

Servlet Interview Question - 5 : -

How can I send user authentication information while making URL Connection?

Servlet Interview Answer - 5 : -

You'll want to use HttpURLConnection.setRequestProperty and set all the appropriate headers to HTTP authorization.
 

Servlet Interview Question - 6 : -

When we don't write any constructor for the servlet, how does container create an instance of servlet?

Servlet Interview Answer - 6 : -

Container creates instance of servlet by calling Class.forName(className).newInstance().

 

 

Servlet Interview Question - 7 : -

Which interface must be implemented by all servlets?

Servlet Interview Answer - 7 : -

Servlet interface.
 

Servlet Interview Question - 8 : -

What is filter? Can filter be used as request or response?

Servlet Interview Answer - 8 : -

A filter is a reusable piece of code that can transform the content of HTTP requests,responses, and header information. Filters do not generally create a response or respond to a request as servlets do, rather they modify or adapt the requests for a resource, and modify or adapt responses from a resource.
 

Servlet Interview Question - 9 : -

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

Servlet Interview Answer - 9 : -

Yes , of course you can use the constructor instead of init(). There's nothing to stop you. But you shouldn't. The original reason for init() was that ancient versions of Java couldn't dynamically invoke constructors with arguments, so there was no way to give the constructur 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.
 

Servlet Interview Question - 10 : -

What is new in ServletRequest interface ? (Servlet 2.4)

Servlet Interview Answer - 10 : -

The following methods have been added to ServletRequest 2.4 version:
public int getRemotePort()
public java.lang.String getLocalName()
public java.lang.String getLocalAddr()
public int getLocalPort()
 

Servlet Interview Question - 11 : -

When using servlets to build the HTML, you build a DOCTYPE line, why do you do that?

Servlet Interview Answer - 11 : -

I know all major browsers ignore it even though the HTML 3.2 and 4.0 specifications require it. But building a DOCTYPE line tells HTML validators which version of HTML you are using so they know which specification to check your document against. These validators are valuable debugging services, helping you catch HTML syntax errors.
 

Servlet Interview Question - 12 : -

Difference between GET and POST ?

Servlet Interview Answer - 12 : -

In GET your entire form submission can be encapsulated in one URL, like a hyperlink. query length is limited to 260 characters, not secure, faster, quick and easy.
In POST Your name/value pairs inside the body of the HTTP request, which makes for a cleaner URL and imposes no size limitations on the form's output. It is used to send a chunk of data to the server to be processed, more versatile, most secure.
 

Servlet Interview Question - 13 : -

What is session?

Servlet Interview Answer - 13 : -

The session is an object used by a servlet to track a user's interaction with a Web application across multiple HTTP requests.
 

Servlet Interview Question - 14 : -

When is the servlet instance created in the life cycle of servlet? What is the importance of configuring a servlet?

Servlet Interview Answer - 14 : -

An instance of servlet is created when the servlet is loaded for the first time in the container. Init() method is used to configure this servlet instance. This method is called only once in the life time of a servlet, hence it makes sense to write all those configuration details about a servlet which are required for the whole life of a servlet in this method.
 

Servlet Interview Question - 15 : -

If a servlet is not properly initialized, what exception may be thrown?

Servlet Interview Answer - 15 : -

During initialization or service of a request, the servlet instance can throw an UnavailableException or a ServletException.