Servlet with Tomcat

 



The Schools Tomcat server works on port 8181. Currently you could visit school's Tomcat as following URL
          
  http://www.labunix.uqam.ca:8181
You will see the page "Tomcat v3.2.1", and you will find API of servlet and JSP, as well as some examples.

Let's now follow the below steps to put a "hello" servlet on Tomcat server.  

1.     In one of my classes, I have taught how to put up your own web page in the school's account. We have created a directory public_html with chmod 755. Check your unix account, if you haven't done it, do as follows:

% cd
% mkdir public_html
% chmod 755 public_html
 

2.     Under public_html, create directory WEB-INF

% cd
% cd public_html
% mkdir WEB-INF
% chmod 711 WEB-INF

Tomcat will automatically search servlet in this directory. 

3.     Under public_html/WEB-INF, create directory classes

% cd
% cd public_html/WEB-INF
% mkdir classes
% chmod 755 classes
 

 

 

4.     Create a file web.xml and save it in public_html/WEB-INF



    %chmod 644 web.xml

for hello, we use the file of web.xml as follows.

 

<?xml version="1.0" encoding="ISO-8859-1"?>

                         <!DOCTYPE web-app
                         PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
                         "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">

                         <web-app>
                             <servlet>
                                 <servlet-name>
                                     hello
                                 </servlet-name>
                                 <servlet-class>
                                     hello
                                 </servlet-class>
                             </servlet>

                         </web-app>

5.     Create a servlet file now. For  this hello example  let's create hello.java, and save it in the directory of  public_html/WEB-INF/classes
 hello.java.


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

public class hello extends HttpServlet

{

  public void doGet(HttpServletRequest request, 
         HttpServletResponse response)
               throws IOException,  ServletException

   {
         response.setContentType("text/html");
         PrintWriter out = response.getWriter();
         out.println("<html><head>");
         out.println("<title>Servlet Test</title>");
         out.println("</head><body>");
         out.println("Hello! Visit ");
         out.println("<a href=\"http://www.google.com\">");
         out.println("Google, a powerful search engine </a>");
         out.println("</body>");
         out.println("</html>");
     }
}

6.     Complie hello.java using the following command.

javac hello.java
 

7.     Set permission for hello.class:

chmod 755 hello.class

Before you do the following steps, please double check what you have done so far, be sure that no errors in those files. ! 

8.     Visit this URL

    http://www.labunix.uqam.ca/~your code here/servlet/hello
 

9.     You can go /var/log/tomcat.log to see any problem when you run your program.