Generic and Http Servlets

Home  »  Java  »  Advance Java  »  Java Servlets  »  Generic and Http Servlets


All the servlet classes are packaged in the javax.servlet package that implements the javax.servlet.servlet interface. The javax.servlet package comprises the GenericServlet class that provides implementations of various methods used during the servlet life cycle. The GenericServlet implements various interfaces such as Servlet and ServletConfig. Table 24.3 lists various interfaces that are included in the javax.servlet package.

Interface
Description
ServletDeclares the methods used during the servlet life cycle.
ServletConfigAllows servlet to receive initialization parameters.
ServletContextEnables servlet to store information about the servlet and its environment in the log files.
ServletRequestTo obtain the data from the client request.
ServletResponseTo give response to the client
SingleThreadModelEnsures that the servlet uses the single thread model.
Table 24.3 Various methods in the javax.servlet package


The service() method of a GenericServlet is invoked when the client sends a request for the GenericServlet method. The syntax of the service() method for the GenericServlet is:



Diagram 24.13 shows the basic operations involved in invoking a GenericServlet method.



Diagram 24.13 Using GenericServlet



The GenericServlet method receives the client's request using the ServletRequest object and sends the response back to the client using the ServletResponse object of the Jav servlet. Table 24.4 lists the various core classes included in the javax.servlet package are.


Class
Description
GenericServletHelps in implementing the interfaces such as Servlet and ServletConfig
ServletInputStreamProvides input stream to obtain the data from the client request.
ServletOutputStreamProvides output stream to give response to the client.
ServletExceptionNotifies when a servlet error occurred.
UnavailableExceptionDetermines that the servlet is unavailable.


Table 24.4 Various classes in the javax.servlet package


To execute the GenericServlet compile the following Hello.java file.



This Hello class import classes from the javax.servlet package that contain classes and interfaces to execute the servlet. The Hello class extends the GenericServlet class, which provides functionality to handle requests responses fromt the client. The Hello class overrides the service method of the GenericServlet class. The setContentType method determines the MIME type of the HTTP response. The PrintWriter object provides response to client in the form of HTTP response. To execute the servlet, start the Tomcat server from Start -> Programs Menu. Open the web browser window and type the following URL:




You will browser the output wher Hello! This is a simple servlet is displayed in Bold in the browser window.


Diagram 24.14 GenericServlet



The HttpServlet class extends from the GenericServlet class of Java. It is included in the javax.servlet.http package. The HttpServlet automatically implements the service() method of the Java servlet to handle server-side requests. Table 24.5 lists the various core interfaces provided by the javax.servlet.http package.
Interface
Description
HTTPServletRequestHelps the servlet to read data from HTTP request.
HTTPServletResponseHelps the servlet to write data to the HTTP response.
HTTPSessionAllows the data to read and written during the session.
HTTPSessionBindingListenerHelps determining whether the object is bound to session or not.
Table 24.5 Various core interfaces in the javax.servlet package

The HttpServlet reads the method type used to submit the values of the form variables. The method used to submit values determines the method of Java servlet to invoke. If the GET method is used to pass form values, the doGet() method of HttpServlet is invoked. The doPost() method of HttpServlet is invoked to handle the POST method.


Diagram 24.14 shows and HttpServlet Interface.


Diagram 24.15 HttpServlet



There are various methods of the HttpServlet service() method. You can override any of theses methods to define the functionality of HttpServlet.

Table 24.5 lists various methods used by HTTPServletRequest Interface.

Method
Description
String getAuthTypeReturns the authenticaton scheme of the servlet.
long getDateHeader(String field)Returns the value of the dateheader field.
String getHeader(String field)Returns the value of the header field.
String getMethod()Returns the method of the HTTP request.
String getPathInfo()Returns the path that is located after the servlet path.


Table 24.5 Various core interfaces in the javax.servlet package

Table 24.6 lists various methods used by HTTPServletResponse Interface.

Method
Description
boolean containsHeader(String field)Returns the true value if the header of the HTTP response contains a field.
String encodeURL(String url)Determines if the session ID must be encoded in the URL to process the methods for the generated URL.
String encodeRedirectURL(String url)Determines if the session ID must be encoded in the URL to process the URL passed to SendRequest() method.
void setDateHeader(String field, long msec)Adds date to the header field.
void setHeader(String field, String value)Adds field to the header.


Table 24.6 Various methods used by HTTPServletResponse Interface

A HTTP servlet is invoked when a user submits a web form. The web form is defined using the Colour.htm file. The colour.htm file prompts the user to select one of three colours and submit the form. The following listing determines the code for the Colour.htm file.






Diagram 24.16 Output Colour.htm



The source code for the MyColour.java file is shown in the following listing. The doGet method of the HttpServlet class is overridden by the MyColour class to process HTTP GET requests send to the server by the client. The getParameter method determines the colour selection made by user.




Diagram 24.17 Output MyColour.class



To execute the above HTTP servlet, start the Tomcat server using the Start->Programs. Open the Colour.htm file in the web browser. Select a colour such as Black, White or Blue and Submit the web form. When the user select the white colour and submit the form, the URL sent to the server will be:




Java Servlet Programming







3 comments: