Servlet Interview Questions and Answers
Question - 81 : - What is the purpose of RequestDispatcher Interface?
Answer - 81 : -
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.
Question - 82 : - Can you call a jsp from the servlet?
Answer - 82 : -
Yes, one of the way is RequestDispatcher interface for example:
RequestDispatcher rd=request.getRequestDispatcher("/login.jsp");
rd.forward(request,response);
Question - 83 : - What is difference between ServletConfig and ServletContext?
Answer - 83 : -
The container creates object of ServletConfig for each servlet whereas object of ServletContext is created for each web application.
Question - 84 : - What is Session Tracking?
Answer - 84 : -
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.
Question - 85 : - What is the use of attribute in servlets?
Answer - 85 : -
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.
Question - 86 : - How to create war file?
Answer - 86 : -
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.
jar -cvf abc.war *
Now all the files of current directory will be converted into abc.war file.
Question - 87 : - What is war file?
Answer - 87 : -
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.
Question - 88 : - How does a servlet get access to its init parameters?
Answer - 88 : -
The getInitParameter() method is used by the servlet in order to get access to its init parameters:
public String ServletConfig.getInitParameter(String name)
The above method returns the value of the named init parameter or if the named init parameter does not exist it will return null. The value returned is always a single string. The servlet then interprets the value.
Question - 89 : - What do you mean by Servlet chaining?
Answer - 89 : -
Servlet Chaining is a way where the output of one servlet is piped to the input of another servlet, and the output of that servlet can be piped to the input of yet another servlet and so on. Each servlet in the pipeline can either change or extend the incoming request. The response is returned to the browser from the last servlet within the servlet chain. In the middle, the output out of each servlet is passed as the input to the next servlet, so every servlet within the chain has an option to either change or extend the content. The figure below represents this. Servlets can help in creating content via servlet chaining.
Question - 90 : - What do you mean by ‘filtering’ in servlets?
Answer - 90 : -
There are usually 2 ways during which one will trigger a series of servlets for an associate incoming request. In the first manner, it is such that the server that bound URLs ought to be handled with the associated specified chain. the other manner is that one will inform the server to redirect all the output of a selected content through a selected servlet before it's returned to the client. This effectively creates a series on the fly. once a servlet transforms one sort of content into another, this method is named filtering.