Ans: A Servlet is a Java program that runs on a Web server. It is similar to an applet but is processed on the server rather than a client’s machine. Servlets are often run when the user clicks a link, submits a form, or performs another type of action on a website
Ans: A cookie is a piece of information that is present between 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.
Ans: PrintWriter is basically a character-stream class. On the other hand, 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.
Ans: Servlet mapping is a process of defining an association between a URL pattern and a servlet. The mapping is used to map requests to Servlets.
Ans: A common feature between Generic Servlet and HTTP Servlet is both these Classes are Abstract Classes. But, they do have differences between them which discussed as follows
Generic Servlet: Protocol Independent | Belongs to javax.servlet package | supports only service() method
HTTP Servlet: Protocol Specific | Belongs to javax.servlet.http package | supports doGet(), doPost(), doHead() methods
Ans: The RequestDispatcher interface defines the object that receives the request from the client and dispatches it to the resources such as a servlet, JSP, HTML file. The RequestDispatcher interface has the following two methods:
public void forward(ServletRequest request, ServletResponse response) Forwards request from one servlet to another resource like servlet, JSP, HTML etc.
public void include(ServletRequest request, ServletResponse response) Includes the content of the resource such as a servlet, JSP, and HTML in the response.
Example:
1 : RequestDispatcher reqdis=request.getRequestDispatcher("log.jsp"); 2 : reqdis.forward(request,response);
Ans: A Filter is defined as a pluggable object that is invoked either at the pre-processing or post-processing of a request.
Logging the request parameters to log files. Authentication and Authorization of the request for the needed resources. Formatting of the request body/header before sending it to the servlet. Compressing response data sent to the client. Change the response by adding some cookies and header information
Ans: The init() method is used to create or load some data that will be used throughout the life of the servlet.
Ans: The load-on-startup element of servlet in web.xml is used to load the servlet at the time of deploying the project or the server to start. This saves time for the response of the first request.
Ans: 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.
1 Ans: Whenever a request for a servlet is placed, then the servlet container will initialize the servlet and load it. This process is defined in our config file called web.xml. But, by default, Container will not initialize the servlet, when the context is loaded. This can be achieved by defining the servlet in a pre-initialization procedure syntax 1. Then, the servlet that we have defined in this tag will be initialized at the start when the context gets loaded before even getting the request.