Goldsmiths College Home PageDepartment of Computing Home Page


JavaServer Pages and Java Servlets

This document describes the requirements for deploying Java servlets and JSP-enabled web pages on the Department of Computing web-server java.doc.gold.ac.uk. It does not describe the JavaServer Pages or Java Servlet specifications (see the links at the bottom of this page for those) and is not a tutorial on how to create servlets or JSP-enabled web pages. Assuming you have created your servlets or JSP pages, this document explains where they should be placed and how they can be accessed via the web.

The JSP/Servlet Engine

The JSP/servlet engine (Apache Tomcat) is running on host java.doc.gold.ac.uk. It can serve .html and .jsp pages from the public_html subdirectory in your home directory. It can also run Java servlets defined in a subdirectory of public_html called WEB-INF. You may already have one or both of these directories if you have created other types of web pages on the server (.html, .php, etc). If not, just create them and make sure their permissions allow public access.

To access your public_html directory via tomcat, you append your username to the hostname, like this:

    http://java.doc.gold.ac.uk/ma123ab

Note that you do not need to put a '~' before your username.

JavaServer Pages

The filenames of your JSP pages should be given the extension .jsp. For example, if user ma012zx had a JSP page called Hello.jsp in their public_html directory, it would be activated by the URL:

    http://java.doc.gold.ac.uk/ma012zx/Hello.jsp

Java Servlets

The servlet engine expects to find the .class files for your Java servlets (or JavaBeans) in the directory:

    public_html/WEB-INF/classes

You will need to create this directory if it doesn't already exist. Your .class files should then be copied into the classes directory.

You can include extra or third-party packages (or any other .jar file) in your applications by placing them into the lib directory:

    public_html/WEB-INF/lib

To configure your servlet for web access, you need to tell tomcat how to run it. This is done by creating a file in your WEB-INF directory called web.xml (or updating it if it's already there). The file needs to specify at least two things about your servlet; the servlet class name and the URL pattern to match. See this example web.xml file for details of how to do that.

To access a servlet via the web, you just add the URL pattern you specified for the servlet in the web.xml file after your username in the tomcat URL. For example, a servlet called 'Hello.class' in the 'public_html/WEB-INF/classes' directory of user ma012zx that is defined with a URL pattern of /Hello in web.xml would be referenced using the URL:

    http://java.doc.gold.ac.uk/ma012zx/Hello

Servlet Tips

Do ensure that the server has access to your files:

The servlet engine has no special access permissions to read the files that you put into your servlet context (WEB-INF) folder. This means that it will be unable to read files that are not world-readable, even if you are able to access them normally yourself. This applies to .class files in WEB-INF/classes and also to .jar files in WEB-INF/lib. To make sure that the server can read your files, enter the following at the server command-prompt:

    chmod -R a+r ~/public_html/WEB-INF

This will add 'read' permissions to all files in your WEB-INF folder and its sub-folders.

You also need to make sure that your public_html folder is itself readable by tomcat. In most cases (where your public_html was created for you by the system), the following command will do this:

    chmod g+r ~/public_html
Do include handlers for both doGet() and doPost() methods:

Unless you are absolutely sure your servlet will always be called using just one of these methods (GET or POST), you should always include both handlers in your servlets. The doPost() handler is used if your servlet is called from an HTML form that specifies the POST method. In all other cases, the doGet() handler is used. In general, it may not particularly matter to your servlets how they are called, so you can just put your main code into one of them (eg. doPost()) and make the other one (doGet()) jump to that.

Further Information