Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Javaee6 service method in servlet

Related Tutorials


 
 

Share on Google+Share on Google+

service method in servlet

Advertisement
In this tutorial you will learn about the service method in servlet that how can it be defined, how to get information from requests , and how the response can be constructed.

service method in servlet

In this tutorial you will learn about the service method in servlet that how can it be defined, how to get information from requests , and how the response can be constructed.

service method is a method of servlet class which is to be written for providing service to a client. It does write in GenericServlet's service doXXXX() method (in doXXXX() method there can be get, post, delete, put etc in place of XXXX)  of an HttpServlet object or could be the any type of protocol-specific methods specified in a class which is implemented by the Servlet interface. Generally, information is extracted from the request object, getting the external resources according to the information and then a response is populated. But in HTTP servlets the following procedure has to be followed to populate the response correctly :

  • should be retrieve the output stream from the response.
  • then filled it into response header
  • and then into an output stream write any body content.

Adding/setting headers after when a response is committed the web container will ignore this attempt. So, it should be noted that the Request header must be set before the commitment of response.

How to get information from request

Request is a data that is passed from the client to a servlet. To get the information from request methods of an interface ServletRequest that is implemented by the every request can get the following information :

  • parameters, that carries the information between clients and servlets
  • Object-valued attributes, that is used to communicate between servlet container and servlet
  • Information of server participation in the request and protocol that is used for conveying the request and also for the client
  • Localization information.

Except these information you can also read an input stream and parse the data manually.

Making responses

Response is a data that is passed from the server to the client. ServletResponse interface is implemented by the every response. Using this interface that defines method which will permits you for the following :

  • you can get an output stream to send the various types of data (character, binary, etc) to the client.
  • you can manage your response data before commitment of response by setContentType(String) method, e.g setContentType("text/html")
  • You can buffer the output with setBufferSize(int) method to write the content before sending back anything to the client.
  • You can set the localization.
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 2, 2011

Related Tutorials

Discuss: service method in servlet  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com