Tech News

Building Web Applications Using Servlets and JSP

Servlets

Servlets are the central processing unit of a Java web application and are responsible for most of the processing required by a web application. Specifically, a servlet is a Java class that implements the javax.servlet.Servlet interface. The Servlet interface defines the methods that all servlets must implement. “One ring to rule them all!” This interface, along with other methods, defines key life-cycle methods such as init(), service(), and destroy() to initialize a servlet, to service requests, and to remove a servlet from the server, respectively. Table 2-1 describes all the methods of the javax.servlet.Servlet interface.

Your First Web Application Using a Servlet

You will develop your first web application in Eclipse in this section. Specifically, you will use Tomcat 7 both as the HTTP server and as the servlet container. You can install Tomcat 7 by downloading the source distribution.

Request Flow for the HelloWorld Servlet

The request originating from the web browser flows through the webserver and the servlet container before the HelloWorld servlet can generate the response, as explained in the following sections.

Examining the Request

When the client (web browser) makes a request (a GET request in this case), the webserver (Tomcat) sees the resource path /helloworld/hello in the request in line 1 and determines that the resource requested by the user is not a static page (for example a .html file) and so forwards the request to the web container (Tomcat). Astute readers will notice that Tomcat serves the role of the webserver and the web container.

Locating the Servlet

The resource path in the request is mapped to the HelloWorld servlet through the web.xml file written in Listing 2-2. This web.xml file is called a deployment descriptor because it describes the deployed servlet to the web container. Through the deployment descriptor, the web container determines the servlet that needs to be called to serve the original HTTP request that the web browser initiated.

Generic Servlet

Most servlets provide similar basic functionality through an abstract javax.servlet.GenericServlet class provided by the Servlet API. The Generic Servlet class implementation is protocol-independent, so it does not matter if it has to respond to HTTP or FTP requests. The GenericServlet abstract class defines an init() method that is called by the default unit(ServletConfig) method to execute any application-specific servlet initialization.

HttpServlet

In a web application, the lack of any protocol-dependent processing in a GenericServlet class signifies that the developer has to write the code for this processing in any subclass she creates. Since HTTP is the most well-known and widely used protocol on the Web, the Servlet API also includes one more abstract subclass of GenericServlet: javax.servlet.HTTP.HttpServlet. The service () method is implemented by HttpServlet, which inspects the incoming HTTP method and invokes the appropriate method for that request type

Last word

As mentioned in the previous section, the superclass of your servlet includes two versions of init(), one that takes a ServletConfig and one that’s a no-arg. The init(ServletConfig) method calls the no-arg to init(), so you need to override only the no-arg version.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button