Commentaires 0

Retranscription du document

You will be using Tomcat which is a web server used to run java servlets. In orderto run your servlet you must start Tomcat, to do this:

1.

Open a command window

2.

Typeset JAVA_HOME=C:\j2sdk1.4.0

[this folder may be sitting withinanother directory e.g. Forte_jdk in which case you will have to alter the path toreflect this], this enables Tomcat to access Java’s classes so that it can run yourservlet.

3.

Ensuring you are in C:\

change your directory toC:\tomcat4\bin

4.

To start Tomcat, typestartup

–

a new command window, called Catalina, shouldopen at this point.

5.

To stop Tomcat running, typeshutdown

in the original command window.

So that Tomcat can access your html and class files, place them within the followingdirectory:

C:\tomcat4\webapps\ProjectName

To clean things up, put your html images into an image directory within this folderand make sure your html looks to this for its images.

To access your servlet from the browser of the computer you’re working on, type:http://localhost:8080/ProjectName/ServletName

To access your servlet from another computer, type:

http://IPAddress:8080/ProjectName/ServletName

To get the IP Address of the computer your servlet is running from, open acommand window and typeipconfig.

Servlet Properties

What is HttpServletRequest?

The HttpServletRequest sends information (i.e. form fields) from the browser to theservlet. It can contain information about the session details. When using forms onyour web site, the information put into the form is sent to the java as a request.The names you give each field on your form will become variable names in therequest and will be used to retrieve the information within the servlet.

On the servlet side, GET methods are handled by the doGet() method and POSTmethods by the doPost(). Essentially doGet() and doPost() do the same thing withinthe servlet, but it is good coding practice to include both within your class and havethe doPost() forward the response and request to the doGet() as below.

The strings you put between the brackets of getParameter() i.e. userName, mustmatch your form fields in your HTML otherwise the servlet will not be able toretrieve the information from the form. From here you will be able to process theinformation within the java as you would with any other variable.

What is HttpServletResponse?

HttpServletResponse is responsible for displaying web pages from java. To set upHttpServletResponse to generate HTML, you need to include the setContentType()and setHeader() methods as below. It’s not important at this stage to know whatthey do but youneed to know to include them.

If you have large amounts of static HTML for the servlet to print out, for examplethe header, navigation and footer of your page, an easier and more efficient way toget java do this is to take advantage of the FileReader class and read your HTML indirectly from a html file rather than writing into your java code.

To do this, you need to segment your HTML so that it can be printed in parts and sothat the content generated by the servlet will fit into the page at its proper place.Save each section as a different HTML file e.g. header.html or footer.html so youcan read each section in individually.

public void doGet(HttpServletRequest request, HttpServletResponse

response) throws IOException, ServletException{

…

String name = request.getParameter(“userName”);

String email = request.getParameter(“email”);

PrintWriter out = response.getWriter();

printBody(1, out);

…

}

public void printBody(int i, PrintWriter out)throws IOException{

int part = i;

String serverPath = “C:/tomcat4/webapps/ProjectName/”;

String file = "";

switch (part){

case 1: file = serverPath + "header.html";

break;

case 2: file = serverPath + "footer.html";

break;

}

BufferedReader read = new BufferedReader(new FileReader(file));

String line = read.readLine();

while(line != null){

out.println(line);

line = read.readLine();

}

}

The statement

printBody(1, out)

calls

the printBody() method which will displayeach line of the html file associated with 1. The PrintWriter out needs to be sent sothat the HTML can be written to the browser.

Reading and Interpreting the Servlet Code

When you get the basic servlet codeit is an excellent idea to read it and try tounderstand what is happening. Some hints to help you with this:



Print out the code, it is far easier to read on paper and you can add your ownnotes.



Read the existing comments.



Try to work out what each method is doing and where it is being used elsewherein the code.



Write your own comments about how you think the code is working. Ask one ofyour tutors to go through your comments with you to ensure that you’re on theright track.



Break down the code into its different sections and make each a new class, sothat more than one person can work on the java. This means you will end upwith a number of different classes working together rather than one huge servletclass.



You will have a main servlet that will

use each different class. For instance, youwould have a servlet called Main and different classes called Student, Providerand Search.