CISE Help & Resources
Running a Java Servlet
Note: Due to the large number of tomcat processes that are started up for class purposes, and then left running after the class is over, we will be killing java servlets at the end of each semester. If you need to continue to run them, simply restart them and they will be good for another semester.
In order to run a java servlet or a JSP, do the following steps:
- Set up the tomcat environment in your home directory. This can
be done by running the commands:
mkdir ~/tomcat cd ~/tomcat tar xf /usr/local/java/tomcat/cise/cise.tar
- Add a java servlet or a JSP.
To add a new java servlet, create a new servlet file. As an example, the file may be called Foo.java. This file must go in the directory:~/tomcat/webapps/servlets-examples/WEB-INF/classes
Next, this file must be compiled.
To add a new JSP, create a new JSP file (Foo.jsp for example) and put it in a new subdirectory in the directory:~/tomcat/webapps/jsp-examples
- Add the servlet or JSP to the
web.xmlfile:~/tomcat/webapps/servlets-examples/WEB-INF/web.xml
Near the otherdefinitions, add:<servlet> <servlet-name>ServletJustAdded</servlet-name> <servlet-class>ClassNameForServletJustAdded</servlet-class> </servlet>And, near the otherdefinitions, add: <servlet-mapping> <servlet-name>ServletJustAdded</servletName> <url-pattern>/servlet/ServletName</url-pattern> </servlet-mapping>Where the url format can be simply/ServletNameor/servlet/ServletName, depending on what URL you would like to use for the servlet when accessing it from a browser.- Add the servlet or JSP to the appropriate index. The index file:
~/tomcat/webapps/servlets-examples/index.html
contains a list of all the java servlets that you can run (including an example from the Tomcat distribution named HelloWorld). Add any servlets you create to this list.
The index file:~/tomcat/webapps/jsp-examples/index.html
contains a list of all the JSPs that you can run (including an example from the Tomcat distribution named dates). Add any JSPs you create to this list.
This step is not strictly necessary, but is conveniant.- Choose a machine and port to run it on. For the machine, you can use any public machine (one of the lab machines for example).
The more important decision is what port you will run tomcat on. For the port, choose a number between 10000 and 65000 (choose it semi-randomly so that it is unlikely that you will conflict with someone else running a servlet).
- Modify the server config file. This file is found in:
~/tomcat/conf/server.xml
Look for the two lines containing:port="8005" port="8080"
Both of these lines must be changed. Change the 8005 to one number, and change the 8080 to a second (different) number. Both should be semi-random between 10000 and 65000. Make a note of the port number that you replace the 8080 with.
- Start up tomcat. This is done by using the command:
tomcat-start
To shutdown tomcat, use the command:tomcat-shutdown
- The following URLs will access tomcat, or a specific servlet or JSP. In each case, MACHINE is the name of the machine you are running tomcat on, PORT is the port number you have chosen to replace 8080 with. SERVLET_NAME is the name of the java servlet (stored in SERVLET_NAME.java), and JSP_DIR and JSP_NAME are the name of the JSP and the directory the JSP is stored in (the JSP file is JSP_DIR/JSP_NAME.jsp).
To access the top level tomcat process, use:http://MACHINE.cise.ufl.edu:PORT/
To access the index of all servlets, use:http://MACHINE.cise.ufl.edu:PORT/servlets-examples/index.html
To access the index of all JSPs, use:http://MACHINE.cise.ufl.edu:PORT/jsp-examples/index.html
To access a specific servlet, use:http://MACHINE.cise.ufl.edu:PORT/servlets-examples/SERVLET_NAME
To access a specific JSP, use:http://MACHINE.cise.ufl.edu:PORT/jsp-examples/JSP_DIR/JSP_NAME.jsp
- Add the servlet or JSP to the appropriate index. The index file:
You can get additional documentation on Tomcat at the Tomcat website. Make sure you read the docuementation for the correct version (currently we are running 5).
You can look at additional examples of servlets and JSPs. Servlets are stored in the two directories:
/usr/local/java/tomcat/webapps/servlets-examples
JSPs are stored in the directory:
/usr/local/java/tomcat/webapps/jsp-examples