Steps to Run Your servlet Program:

  1. Create the following directories and change their permissions in your home directory:
    % cd
    % chmod 711 . 
    % chmod 755 public_html
    % cd public_html
    % mkdir WEB-INF
    % mkdir WEB-INF/classes
    % chmod -R 755 WEB-INF
    
  2. Write your first servlet program in directory ~/public_html/WEB-INF/classes
    % cd ~/public_html/WEB-INF/classes
    % cat Gretting.java
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    
    public class Greeting extends HttpServlet {
    
      public void doGet(HttpServletRequest request,
                        HttpServletResponse response)
        throws ServletException, IOException {
    
        response.setContentType("text/html");
        PrintWriter returnHTML = response.getWriter();
    
        returnHTML.println("<html><head><title>");
        returnHTML.println("A simple GET servlet");
        returnHTML.println("</title></head><body>");
        returnHTML.println("<h2>This is your servlet answering</h2>");
        returnHTML.println("</body></html>");
        returnHTML.close();
      }
    }
    
  3. Setup your JAVA_HOME and CLASSPATH. In C shell, run "source ~cs655/bin/tomcat.csh". In Borne shell, run ". ~cs655/bin/tomcat.sh".
  4. Compile your servlet program in directory ~/public_html/WEB-INF/classes and change the permission of the generated class file.
    % javac Greeting.java
    % chmod a+r Greeting.class
    
  5. Create the servlet deployment descriptor - web.xml in ~/public_html/WEB-INF/. You need to specify the servlets you are using in this file. Otherwise, the Tomcat server has no knowledge of your servlet. For example, you have two classes: Greeting and Store.
    % chmod 644 web.xml
    % cat web.xml
    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <!DOCTYPE web-app
        PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        "http://java.sun.com/dtd/web-app_2_3.dtd">
    
    <web-app>
      <servlet>
         <servlet-name>Greeting</servlet-name>
         <servlet-class>Greeting</servlet-class>
      </servlet>
      <servlet-mapping>
         <servlet-name>Greeting</servlet-name>
         <url-pattern>/Greeting</url-pattern>
       </servlet-mapping>
      <servlet>
         <servlet-name>Store</servlet-name>
         <servlet-class>Store</servlet-class>
      </servlet>
      <servlet-mapping>
         <servlet-name>Store</servlet-name>
         <url-pattern>/Store</url-pattern>
       </servlet-mapping>
    </web-app>
    
    Suggestion: You can specify your servlet before using them. Then you don't have to wait until the server to restart.
  6. Open a Web browser, and type the following URL in location line. For example, use lynx as follows:
    lynx http://kirk.cs.wichita.edu:6555/xyz/Greeting
    
    where xyz is your account name. Type the return key and you will see the "This is your servlet answering" string displayed on the screen.
  7. Create a data file for your servlet:
Note: Your servlet might not be run until the Tomcat server has been restarted. The Tomcat server will be periodically restarted.