More Tutorials
GETTING STARTED WITH JAVA SERVLETS

This document summarizes the steps necessary for preparing, compiling and running your own Java servlets.

The server and servlet engine

A web server is set up for CMPT 310 in 2000-3. It is Apache 1.3.11 and runs on the CSIL Sun server gemini.csil.sfu.ca:8080, port 8080. It is capable of running servlets. The servlet engine is Apache JServ 1.1.2. Documentation is available at http://java.apache.org/

Your servlets zone / directory and URL

Each user has a zone for servlets. This is a directory under which you can place your servlets. It is accessible on gemini.csil.sfu.ca, at

/WWW/users/youruserid/servlets/

Initially this directory is setup for you to contain a servlet example:

http://gemini.csil.sfu.ca:8080/youruserid/servlets/Hello

a makefile in that directory shows the use of javac with the jsdk in the classpath.

Your servlets properties

Each user owns a file in their web area on gemini which belongs to the user and sets the servlet properties.

/WWW/users/youruserid/servlets/youruserid.properties

this file must be readable by the user nobody.

Java Servlet Development Kit 2.0

The jdsk2.0 is located on gemini.csil.sfu.ca, in the /WWW/jsdk2.0 directory. If you want to you use it you need to logon on gemini.csil.sfu.ca. If you want to compile your servlet applications on gemini, use the CLASSPATH /WWW/jsdk2.0/lib/jsdk.jar for using with javac.

A typical steps to compile and running your servlet is as follows:

1. You can create a new Java Servlet document which is just a Java Class implement a special interface, using the methods we learnt from the GETTING STARTED WITH JAVA tutorial. The directory to hold all the servlets for you is /WWW/users/youruserid/servlets/

Below is an example program: Hello471.java

 

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

 

public class Hello470 extends HttpServlet

{ /** * Handle the GET and HEAD methods by building a simple web page.

* HEAD is just like GET, except that the server returns only the

* headers (including content length) not the body we write.

*/

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

{ PrintWriter out;

String title = "Example Apache JServ Servlet";

// set content type and other response header fields first

response.setContentType("text/html");

// then write the data of the response

out = response.getWriter();

out.println("<HTML><HEAD><TITLE>");

out.println(title);

out.println("</TITLE></HEAD><BODY bgcolor=\"#FFFFFF\">");

out.println("<H1>"+title+"</H1>");

out.println("<H2> Hello, CMPT470!<br>");

out.println("</BODY></HTML>");

out.close();

}

}

 

 

2. Log on to gemini.csil.sfu.ca: (in your case, replace tlie by your own Unix ID)

26: tlie_gemini% cd /WWW/users/tlie/servlets

/WWW/users/tlie/servlets

27: tlie_gemini% javac -classpath /WWW/jsdk2.0/lib/jsdk.jar Hello470.java

Launch your browser and go to URL:

http://gemini.csil.sfu.ca:8080/tlie/servlets/Hello470

The servlet will create a dynamic HTML page for you, shows on the screen as:

Example Apache JServ Servlet

Hello, CMPT470!

 

Congradulations! Your have successfully compiled and run your Java Servlet on the web server!

More Tutorials