Servlets Interview Questions and Answers
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. If a servlet is not properly initialized, what exception may be thrown?
During initialization or service of a request, the servlet instance can throw an UnavailableException or a ServletException.
Ques 2. Given the request path below, which are context path, servlet path and path info?
/bookstore/education/index.html
context path: /bookstore
servlet path: /education
path info: /index.html
Ques 3. What is filter? Can filter be used as request or response?
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.
Ques 4. When using servlets to build the HTML, you build a DOCTYPE line, why do you do that?
Ques 5. What is new in ServletRequest interface ? (Servlet 2.4)
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()
Ques 6. Request parameter How to find whether a parameter exists in the request object?
1.boolean hasFoo = !(request.getParameter("foo") == null || request.getParameter("foo").equals(""));
2. boolean hasParameter = request.getParameterMap().contains(theParameter);
(which works in Servlet 2.3+)
Ques 7. How can I send user authentication information while making URL Connection?
You'll want to use HttpURLConnection.setRequestProperty and set all the appropriate headers to HTTP authorization.
Ques 8. Why don't we write a constructor in a servlet?
Container writes a no argument constructor for our servlet.
Ques 9. When we don't write any constructor for the servlet, how does container create an instance of servlet?
Container creates instance of servlet by calling Class.forName(className).newInstance().
Ques 10. What is the difference between callling a RequestDispatcher using ServletRequest and ServletContext?
>We can give relative URL when we use ServletRequest and not while using ServletContext.
Ques 11. What are advantages of servlets over CGI?
In CGI for every request there is a new process started which is quiet an overhead. In servlets JVM stays running and handles each request using a light weight thread. In CGI if there are 5000 request then 5000 CGI program is loaded in memory while in servlets there are 5000 thread and only one copy of the servlet class.
Ques 12. Can you explain in detail "javax.servlet" package?
Interfaces in javax.servlet :-
Servlet Interface: This interface has the init( ), service( ), and destroy( ) methods that are called by the server during the life cycle of a servlet. Following are the method in Servlet interface :-
String getServletName():- Returns the name of the invoking servlet.
Most helpful rated by users:
- What is Servlet?
- Why is Servlet so popular?
- What is servlet container?
- When a client request is sent to the servlet container, how does the container choose which servlet to invoke?
- If a servlet is not properly initialized, what exception may be thrown?