Servlet Interview Questions and Answers
Question - 41 : - What are the life cycle methods of a servlet?
Answer - 41 : -
The Servlet Life Cycle consists of three methods:
- public void init(ServletConfig config): This method is used by the container to initialize the servlet, this method is invoked only once in the lifecycle of the servlet.
- public void service(ServletRequest request, ServletResponse response): This method is called once for every request, a container can’t invoke service() method until unless init() method is executed.
- public void destroy(): This method is invoked once when a servlet is unloaded from memory.
Question - 42 : - Write a Hello World Program using Servlets.
Answer - 42 : -
The code to write a Hello World PRogram using Servlets is as follows:
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
-
- public class HelloWorld extends HttpServlet {
- private String message;
- public void init() throws ServletException {
- message = "Hello World";
- }
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
- out.println("
" + message + "
"); - }
- public void destroy() {
- }
- }
Question - 43 : - Explain MVC pattern.
Answer - 43 : -
Model-View-Controller (MVC) is a design pattern that divides a software application into three segments namely the Model, the View and the Controller.
- A model deals with the behaviour of the application. It contains the data and business logic of the application. It notifies views and controllers when there is a change in its state.
- A view renders the information to the user so that it looks attractive and appealing. It takes information from the model using which it generates output.
- A controller takes input from a user and sends the command to model or view. It controls the flow of the application.
Question - 44 : - How to get the server information in a servlet?
Answer - 44 : -
Yes, we can retrieve the information of a server in a servlet. We can use below code snippet to get the servlet information in a servlet through servlet context object.
getServletContext().getServerInfo()
Question - 45 : - What is a WAR file?
Answer - 45 : - The WAR(Web Application Resource) file specifies the web elements. Either 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.
Question - 46 : - What is the use of HttpServletRequestWrapper and HttpServletResponseWrapper?
Answer - 46 : - Both HttpServletRequestWrapper and HttpServletResponseWrapper classes are used to help developers with a custom implementation of a servlet request and response types. Programmers can extend these classes and override only the specific methods that they need to implement for customized request and response objects.
Question - 47 : - Write a simple Servlet program to print the contents of HTML.
Answer - 47 : -
We can print the contents of HTML using the following steps:
Step 1: Get the object of PrintWriter using request.
1 PrintWriter out = response.getWriter();
Step 2: Now print HTML.
1 out.println("Hello World");
Question - 48 : - What do you mean by InterServlet communication?
Answer - 48 : -
InterServlet communication is a method to invoke another servlet using RequestDispatcherforward() and include() methods and provide additional attributes in the request for other servlet use.
Question - 49 : - What are the different methods involved in the process of session management in servlets?
Answer - 49 : -
The different methods involved in the session management in servlets are as follows:
User Authentication
- A user tries to access a protected resource, such as a JSP page. If the user has been authenticated, the servlet container makes the resource available; otherwise, the user is asked for a username and password
HTML Hidden Field
Cookies
- A small text file created by a website that is stored in the user’s computer either temporarily for that session only or permanently on the hard disk. Cookies provide a way for the website to recognize you and keep track of your preferences
URL Rewriting
- URL rewriting is an automatic process of altering a program written for manipulating the parameters in a URL (Uniform Resource Locator). URL manipulation is employed as a convenience by a Web server administrator, or for nefarious purposes by a hacker.
Session Management API
- Session Management API is built on top of the Request-Response methods for session tracking. Session Tracking is a way to maintain state/data of a user. It is also known as session management in servlet.
Question - 50 : - How do you get the IP address of the client in servlet?
Answer - 50 : -
We can use the following code to get the client IP address in servlet.
1 request.getRemoteAddr()