HomeServlet

Servlet interview questions asked to me in my first job interview

Like Tweet Pin it Share Share Email

In this post we will understand how to answer basic Java Servlet Interview Questions. These questions are simple but your answer can boost or drop your impression on interviewer. I have listed the questions which has been asked to me during my interview. I think I should share few of them with you.

What is the role or function of a Servlet in a web application?

This Servlet interview question contains many information in it. Interviewer may get an idea about your knowledge. Please try to answer in depth. You can cover all the things related to Servlet. Let us read how can should we answer this question.

“The basic duty of a Servlet is to perform the specific action mentioned in its service() method and serve response back to the browser. This response can be created in any formate like a HTML file, PDF, XML, invoking a new url, calling a web service, sending to another url,completing a financial transaction like -bank transactions, online shopping transaction.”

Answer of this question can lead interviewer to ask about “Life Cycle of a servlet”.

How can I convert a Simple Java class into a Generic Servlet?

Answer this interview question as “We can convert a Java class into a servlet class by extending it to javax.servlet.GenericServlet or javax.servlet.http.HttpServlet. In this process we need to override all the methods required for  GenericServlet  or HttpServlet.”
If you want to answer more firmly, take a pen and paper. Write a java program to show your interviewer. You ought to be practical while answering. Let us see one practical example.
A simple Java Class


package com.sitenol.servlet;
public class SimpleJavaClass {
}

Converting this SimpleJavaClass in Servlet using javax.servlet.http.HttpServlet.


package com.sitenol.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleJavaClass extends HttpServlet {
private static final long serialVersionUID = 1L;
public HttpServletSampleClass() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// this method will execute if your form action get
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// this method will execute if your form action post
}
}

Converting this SimpleJavaClass in Servlet using javax.servlet.GenericServlet.


package com.sitenol.servlet;
import java.io.IOException;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class SimpleJavaClass extends GenericServlet {
private static final long serialVersionUID = 1L;
public GenericServletSampleClass() {
super();
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
// In case of Generic servlet service method serve as get and post
// method. You can write your code here.
}
}

Can we initialize a servlet before making request from browser or Servlet Pre-initialization?

Yes, we can initialize a servlet when web container starts. It is known as pre initialization or pre loading of a servlet.

How to pre initialize or pre load a Servlet ?

Here is an example <load-on-startup> configuration:


<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>com.sitenol.servlet.Servlet1</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>com.sitenol.servlet.Servlet2</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>

The <servlet> element has a sub element called <load-on-startup>. We can use  <load-on-startup> to define when the servlet container should load the servlet. In case <load-on-startup> element is missing,  the servlet container typically loads your servlet when the first request arrives for it.

By setting a <load-on-startup> element, you can tell  the servlet container to load the servlet as soon as the servlet container starts. By doing this setting, the servlets init() method is called when the servlet is loaded.

You can assign sequence numbers to servlets in a web application. The number inside the <load-on-startup>1</load-on-startup>
element tells the servlet container in what sequence the  servlets should be loaded. The lower numbers are loaded first.
If the value is negative, or unspecified, the servlet container can load the servlet at any time. In the above scenario servlet1 will initialize before servlet2.

We request to let us know few more interview questions asked to you. We will add few more question from your feedback and from our lab. Please keep on reading.  Please drop your comments if you liked this article. Thanks!

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *