Page #462 (34.2. HTML and Common Gateway Interface)

 
[Page 1160 ( continued )]

34.3. Creating and Running Servlets

To run Java servlets, you need a servlet container. Many servlet containers are available. Tomcat , developed by Apache ( http://www.apache.org/ ), is a standard reference implementation for Java servlets and JavaServer Pages. It can be used standalone as a Web server or be plugged into a Web server like Apache, Netscape Enterprise Server, or Microsoft Internet Information Server. Several versions of Tomcat are available. You should use Tomcat 5 or higher to take advantage of new specifications for servlets and JSP. This book uses Tomcat 5.5.9. You can download Tomcat in one zip file named jakarta-tomcat-5.5.9.zip from http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi .

Note

For more information on obtaining and installing Tomcat 5.5.9, see Supplement VII.E, " Tomcat Tutorial ."


34.3.1. Creating a Servlet

Before introducing the servlet API, it is helpful to use a simple example to demonstrate how servlets work. A servlet resembles an applet to some extent. Every Java applet is a subclass of the Applet class. You need to override appropriate methods in the Applet class to implement the applet. Every servlet is a subclass of the HttpServlet class. You need to override appropriate methods in the HttpServlet class to implement the servlet. Listing 34.1 is a servlet that generates a response in HTML using the doGet method.


[Page 1161]
Listing 34.1. FirstServlet.java
 1   import   javax.servlet.*;  2   import   javax.servlet.http.*;  3  4   public class    FirstServlet  extends  HttpServlet  {  5  /** Handle the HTTP GET method.  6  * @param request servlet request  7  * @param response servlet response  8  */  9   protected void    doGet  (HttpServletRequest request, 10        HttpServletResponse response) 11   throws   ServletException, java.io.IOException { 12  response.setContentType(   "text/html"   );  13      java.io.PrintWriter  out  = response.getWriter(); 14  // output your page here  15      out.println(   "<html>"   ); 16      out.println(   "<head>"   ); 17      out.println(   "<title>Servlet</title>"   ); 18      out.println(   "</head>"   ); 19      out.println(   "<body>"   ); 20      out.println(   "Hello, Java Servlets"   ); 21      out.println(   "</body>"   ); 22      out.println(   "</html>"   ); 23      out.close(); 24    } 25  } 

The doGet method (line 9) is invoked when the Web browser issues a request using the GET method. The doGet method has two parameters, request and response . request is for obtaining data from the Web browser, and response is for sending data back to the browser. Line 12 indicates that data sent back to the browser is text/html. Line 13 obtains an instance of PrintWriter for actually sending data to the browser.

34.3.2. Creating the Context Root Directory

Suppose you have installed Tomcat 5.5.9 or higher in c:\jakarta-tomcat-5.5.9. To run servlets from Tomcat, you have to create a context root and place the servlets and other supporting files under the context root directory. The context root directory must be placed under the Tomcat webapps directory, as shown in Figure 34.3.

Figure 34.3. You have to create directories and files for Web applications in this pattern.
(This item is displayed on page 1162 in the print version)


The webapps directory is automatically created when you install Tomcat. This book creates a context root directory named liangweb . You have to create a folder named WEB-INF under the root directory, then a folder named classes under WEB-INF to hold servlet classes and other supporting classes. If the class uses packages (e.g., package chapter34 ), you need to place the class file in the appropriate folders.

34.3.3. Compiling Servlets

To compile FirstServlet.java, you need to add c:\jakarta-tomcat-5.5.9\common\lib\servlet-api.jar to the classpath from the DOS prompt, as shown below:

 set classpath=%classpath%;c:\jakarta-tomcat-5.5.9  \common\lib\servlet-api.jar 

servlet-api.jar contains the classes and interfaces to support servlets. Use the following command to compile the servlet:

 javac FirstServlet.java 

Copy the resultant .class file into c:\jakarta-tomcat-5.5.9\webapps\liangweb\WEB-INF\classes so that it can be found at runtime.


[Page 1162]

Tip

You can compile FirstServlet directly into the target directory by using the “d option in the javac command:

 javac FirstServlet.java d targetdirectory 


34.3.4. Mapping a Servlet to a URL

Before you can run a servlet in Tomcat 5.5.9, you have to first create the web.xml file with a servlet entry and a mapping entry. This file is located in c:\jakarta-tomcat-5.5.9\webapps\liangweb\WEB-INF\web.xml . If the file already exists, insert a servlet entry and a mapping entry into web.xml for the servlet.

The servlet entry declares an internal servlet name for a servlet class using the following syntax:

   <servlet>     <servlet-name>    Internal Name    </servlet-name>     <servlet-class>    servlet class name    </servlet-class>     </servlet>   

The map entry maps an internal servlet name with a URL using the following syntax:

   <servlet-mapping>     <servlet-name>    Internal Name    </servlet-name>     <url-pattern>    URL    </url-pattern>     </servlet-mapping>   


[Page 1163]

For example, before running FirstServlet.class, you may insert the following lines to the web.xml file:

   <web-app>      <servlet>       <servlet-name>   FirstServlet   </servlet-name>       <servlet-class>   FirstServlet   </servlet-class>       </servlet>       <servlet-mapping>       <servlet-name>   FirstServlet   </servlet-name>       <url-pattern>   /FirstServlet   </url-pattern>       </servlet-mapping>      </web-app>   

Note

For your convenience, I have created the web.xml that contains the descriptions for running all the servlets in this chapter. You can download it from www.cs.armstrong.edu/liang/intro6e/supplement/web.xml.


Tip

You can use an IDE such as NetBeans, Eclipse, or JBuilder to simplify the development of Web applications. The tool can automatically create the directories and files. For more information, see the tutorials on NetBeans, Eclipse, and JBuilder on the Companion Website.


Tip

You can deploy a Web application using a Web archive file (WAR). For more information, see Supplement VII.E, " Tomcat Tutorial ."


34.3.5. Starting and Stopping Tomcat

Before running the servlet, you need to start the Tomcat servlet engine. To start Tomcat, you have to first set the JAVA_HOME environment variable to the JDK home directory using the command given below. Please note that there is no space before or after the = sign in the following line:

 set JAVA_HOME=c:\Program Files\java\jdk1.5.0 

The JDK home directory is where your JDK is stored. On my computer, it is c:\ProgramFiles\jdk1.5.0 . You may have a different directory. You can now start Tomcat using the command startup from c:\jakarta-tomcat-5.5.9\bin , as follows :

 c:\jakarta-tomcat-5.5.9\bin>   startup   

Note

By default, Tomcat runs on port 8080. An error occurs if this port is currently being used. You can change the port number in c:\jakarta-tomcat-5.5.9\conf\server.xml .


Note

To terminate Tomcat, use the shutdown command from c:\jakarta-tomcat-5.5.9\bin .


To prove that Tomcat is running, type the URL http://localhost:8080 from a Web browser, as shown in Figure 34.4.


[Page 1164]
Figure 34.4. The default Tomcat page is displayed.

34.3.6. Running Servlets

To run the servlet, start a Web browser and type http://localhost:8080/liangweb/FirstServlet in the URL, as shown in Figure 34.5.

Figure 34.5. You can request a servlet from a Web browser.

Note

You can use the servlet from anywhere on the Internet if your Tomcat is running on a host machine on the Internet. Suppose the host name is liang.armstrong.edu; use the URL http://liang.armstrong.edu:8080/liangweb/FirstServlet to test the servlet.


Note

If you have modified the servlet, you need to shut down and restart Tomcat.


 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net