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 doc.gold.ac.uk (aka 'igor'). It does not describe the JavaServer Pages or Java Servlet specifications 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 referenced via the web.

The JSP/Servlet Engine

The JSP/servlet engine is running on host doc.gold.ac.uk at port 8888. Since web browsers connect to port 80 by default, this means that a URL used to reference your servlets and JSP-enabled web pages must specify the port number along with the hostname (and your username), as follows:

  http://www.doc.gold.ac.uk:8888/~username

JavaServer Pages

The JSP/servlet engine looks for your JSP pages in a directory called public_html in your home directory on the server. This directory may already exist if you have created other types of web pages on the server (.html, .php, etc). If it does not exist, you should create it and ensure that it is publicly accessible. The filenames of your JSP pages should be given the extension .jsp.

So, for example, if user ma012zx had a JSP page called Hello.jsp in their public_html directory, it would be referenced using the URL:

  http://www.doc.gold.ac.uk:8888/~ma012zx/Hello.jsp

Java Servlets

The JSP/servlet engine expects to find the .class files for your Java servlets (or JavaBeans) in the directory public_html/WEB-INF/classes. This means you need to create a directory inside your public_html directory called WEB-INF and then in the WEB-INF directory you need to create a directory called classes. 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 a folder called lib which you should create in your WEB-INF folder.

To reference a servlet via the web, you simply add '/servlet/' to the end of your 'home' URL and then append the name of the .class file (without the .class extension). So, for example, a servlet called 'Hello.class' in the 'public_html/WEB-INF/classes' directory of user ma012zx would be referenced using the URL:

  http://www.doc.gold.ac.uk:8888/~ma012zx/servlet/Hello

Servlet Do's and Don'ts

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 (in fact, an unreadable .jar file can prevent your context from initializing at all!). 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.


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 probably won't 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.


Do not use the output streams in the java.lang.System package in your servlets:
A servlet does not have normal System.out and System.err streams and should not write to them. All output produced by the servlet must be sent to the stream returned by HttpServletResponse.getWriter(), whether the servlet completes its task successfully or not.


Do not call java.lang.System.exit() to terminate your servlet:
A servlet is not like a normal application that has an end-point at which it terminates. It is intended to run continuously as part of the server itself. If a servlet has a problem that prevents it from executing normally (such as an exception), it should write some kind of report indicating the problem to the HttpServletResponse.getWriter() stream and then return normally. It should never call an exit() method of any kind, as this can cause problems for the server itself.


Further Information


Last modified on 9th April 2003 by Eamonn Martin