Prepare for Servlet interviews with questions on lifecycle, request handling, and web development.
There is a list of 30 servlet interview questions for beginners and professionals. If you know any servlet interview question that has not been included here, kindly post your question in the Ask Question section.
Only one object at the time of first request by servlet or web container.
| Method | Description |
|---|---|
| public void init(ServletConfig config) | It is invoked only once when first request comes for the servlet. It is used to initialize the servlet. |
| public void service(ServletRequest request,ServletResponse)throws ServletException,IOException | It is invoked at each request.The service() method is used to service the request. |
| public void destroy() | It is invoked only once when servlet is unloaded. |
The web container or servlet container.
At the time of first request.
| Get | Post |
|---|---|
| 1) Limited amount of data can be sent because data is sent in header. | Large amount of data can be sent because data is sent in body. |
| 2) Not Secured because data is exposed in URL bar. | Secured because data is not exposed in URL bar. |
| 3) Can be bookmarked | Cannot be bookmarked |
| 4) Idempotent | Non-Idempotent |
| 5) It is more efficient and used than Post | It is less efficient and used |
PrintWriter is a character-stream class where as ServletOutputStream is a byte-stream class. The PrintWriter class can be used to write only character-based information whereas ServletOutputStream class can be used to write primitive values as well as character-based information.
The GenericServlet is protocol independent whereas HttpServlet is HTTP protocol specific. HttpServlet provides additional functionalities such as state management etc.
When one servlet communicates to another servlet, it is known as servlet collaboration. There are many ways of servlet collaboration:
The RequestDispacher interface provides the facility of dispatching the request to another resource it may be html, servlet or jsp. This interceptor can also be used to include the content of antoher resource.
more details...Yes, one of the way is RequestDispatcher interface for example:
| forward() method | sendRedirect() method |
|---|---|
| 1) forward() sends the same request to another resource. | 1) sendRedirect() method sends new request always because it uses the URL bar of the browser. |
| 2) forward() method works at server side. | 2) sendRedirect() method works at client side. |
| 3) forward() method works within the server only. | 3) sendRedirect() method works within and outside the server. |
The container creates object of ServletConfig for each servlet whereas object of ServletContext is created for each web application.
Session simply means a particular interval of time.
Session Tracking is a way to maintain state of an user.Http protocol is a stateless protocol.Each time user requests to the server, server treats the request as the new request.So we need to maintain the state of an user to recognize to particular user.
more details...A cookie is a small piece of information that is persisted between the multiple client requests. A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.
more details...Cookie works at client side whereas HttpSession works at server side.
A filter is an object that is invoked either at the preprocessing or postprocessing of a request. It is pluggable.
more details...By the help of ServletContextListener interface.
It will not work if cookie is disabled from the browser.
more details...One of the way is by MultipartRequest class provided by third party.
more details...The load-on-startup element of servlet in web.xml is used to load the servlet at the time of deploying the project or server start. So it saves time for the response of first request.
more details...It will not affect the container, now servlet will be loaded at first request.
more details...A war (web archive) file specifies the web elements. A servlet or jsp project can be converted into a war file. Moving one servlet project from one place to another will be fast as it is combined into a single file.
more details...The war file can be created using jar tool found in jdk/bin directory. If you are using Eclipse or Netbeans IDE, you can export your project as a war file.
To create war file from console, you can write following code.
Now all the files of current directory will be converted into abc.war file.
more details...There are mainly 3 annotations used for the servlet.
ServletContextEvent.
more details...HttpSessionEvent.
more details...ServletContextAttributeEvent.
It is used to specify the welcome file for the project.
more details...Attribute is a map object that can be used to set, get or remove in request, session or application scope. It is mainly used to share information between one servlet to another.
more details...We request you to subscribe our newsletter for upcoming updates.