- 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
- 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();
}
}
- Setup your JAVA_HOME and CLASSPATH. In C shell, run
"source ~cs655/bin/tomcat.csh". In Borne shell, run
". ~cs655/bin/tomcat.sh".
- 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
- 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.
- 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.
- Create a data file for your servlet:
- If it is created in your public_html directory, then specify
File data = new File("../webapps/xyz/data.dat")
where xyz is your account name and data.dat should be world writable.
- If it is created in your public_html/WEB-INF/classes directory, then
specify File data = new File("../webapps/xyz/WEB-INF/classes/data.dat")
where xyz is your account name and data.dat should be world writable.
- If it is created in the ~cs655/tomcat/data directory, then specify
File data = new File("../data/xyzdata.dat")
where xyzdata.dat should have a unique file name so that it won't mix
up with other's files.