Chapter: Java The Complete Reference - Applying Java - Introducing Servlets

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

A Simple Servlet

To become familiar with the key servlet concepts, we will begin by building and testing a simple servlet.

A Simple Servlet

 

To become familiar with the key servlet concepts, we will begin by building and testing a simple servlet. The basic steps are the following:

 

Create and compile the servlet source code. Then, copy the servlet’s class file to the proper directory, and add the servlet’s name and mappings to the proper web.xml file.

 

            Start Tomcat.

 

            Start a web browser and request the servlet.

 

Let us examine each of these steps in detail.

 

Create and Compile the Servlet Source Code

 

To begin, create a file named HelloServlet.java that contains the following program:

 

import java.io.*; import javax.servlet.*;

 

public class HelloServlet extends GenericServlet {

 

public void service(ServletRequest request, ServletResponse response)

 

throws ServletException, IOException { response.setContentType("text/html"); PrintWriter pw = response.getWriter(); pw.println("<B>Hello!");

 

pw.close();

 

}

 

}

 

Let’s look closely at this program. First, note that it imports the javax.servlet package. This package contains the classes and interfaces required to build servlets. You will learn more about these later in this chapter. Next, the program defines HelloServlet as a subclass of GenericServlet. The GenericServlet class provides functionality that simplifies the creation of a servlet. For example, it provides versions of init( ) and destroy( ), which may be used as is. You need supply only the service( ) method.

Inside HelloServlet, the service( ) method (which is inherited from GenericServlet) is overridden. This method handles requests from a client. Notice that the first argument is a ServletRequest object. This enables the servlet to read data that is provided via the client request. The second argument is a ServletResponse object. This enables the servlet to formulate a response for the client.

The call to setContentType( ) establishes the MIME type of the HTTP response. In this program, the MIME type is text/html. This indicates that the browser should interpret the content as HTML source code.

Next, the getWriter( ) method obtains a PrintWriter. Anything written to this stream is sent to the client as part of the HTTP response. Then println( ) is used to write some simple HTML source code as the HTTP response.

Compile this source code and place the HelloServlet.class file in the proper Tomcat directory as described in the previous section. Also, add HelloServlet to the web.xml file, as described earlier.

Start Tomcat

Start Tomcat as explained earlier. Tomcat must be running before you try to execute a servlet.

 

Start a Web Browser and Request the Servlet

 

Start a web browser and enter the URL shown here:

 

http://localhost:8080/examples/servlets/servlet/HelloServlet

 

Alternatively, you may enter the URL shown here:

 

http://127.0.0.1:8080/examples/servlets/servlet/HelloServlet

 

This can be done because 127.0.0.1 is defined as the IP address of the local machine.

 

You will observe the output of the servlet in the browser display area. It will contain the string Hello! in bold type.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.