Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

Servlets Interview Questions and Answers For Freshers Part-2

Be First!

servlet11.For what purpose destroy() method of a servlet is used?
This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities.

12.For what purpose doGet() method of a servlet is used?
This method should be used to get data from server.

13.For what purpose doPost() method of a servlet is used?
This method should be used to process data on the server.

14.Explain working of service() method of a servlet.
The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client.

Each time the server receives a request for a servlet, the server spawns a new thread and calls service. The service() method checks the HTTP request type (GET, POST, PUT, DELETE, etc.) and calls doGet, doPost, doPut, doDelete, etc. methods as appropriate.

Here is the signature of this method:

public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException{
}
The service () method is called by the container and service method invokes doGe, doPost, doPut, doDelete, etc. methods as appropriate. So you have nothing to do with service() method but you override either doGet() or doPost() depending on what type of request you receive from the client.

15.How to read form data in servlet?
Servlets handles form data parsing automatically using the following methods depending on the situation:

getParameter(): You call request.getParameter() method to get the value of a form parameter.

getParameterValues(): Call this method if the parameter appears more than once and returns multiple values, for example checkbox.

getParameterNames(): Call this method if you want a complete list of all parameters in the current request.

16.How to read name of all parameters in servlet?
getParameterNames() method of HttpServletRequest returns complete list of all parameters in the current request. This method returns an Enumeration that contains the parameter names in an unspecified order.

Once we have an Enumeration, we can loop down the Enumeration in the standard manner, using hasMoreElements() method to determine when to stop and using nextElement() method to get each parameter name.

17.How to read http header information in servlet?
We can use getHeaderNames() method of HttpServletRequest to read the HTTP header infromation. This method returns an Enumeration that contains the header information associated with the current HTTP request.

Once we have an Enumeration, we can loop down the Enumeration in the standard manner, using hasMoreElements() method to determine when to stop and using nextElement() method to get each parameter name.

18.What is HTTPServletRequest class?
When a browser requests for a web page, it sends lot of information to the web server which can not be read directly because this information travel as a part of header of HTTP request. HTTPServletRequest represents this HTTP Request.

19.What is HTTPServletResponse class?
when a Web server responds to a HTTP request to the browser, the response typically consists of a status line, some response headers, a blank line, and the document. HTTPServletResponse represents this HTTP Response.

20.How to write html contents using servlets?
Get the object of PrintWriter using request.

PrintWriter out = response.getWriter();
Now print html.

out.println(“Hello World”);

(148)

Previous
Next

Leave a Reply