Question Description

Building a Servlet Using Eclipse

For the most part, building a servlet on Eclipse is done thesame way as you would build any other Java class. Simply create a project foryour class then add a class to your project. Place the servlet code into yourclass. As an example, add a class to your project called FirstServlet. Thencopy the following code into your class.

import java.io.*;

importjavax.servlet.*;

importjavax.servlet.http.*;

public class FirstServlet extends HttpServlet{

public void doGet (HttpServletRequestrequest,

HttpServletResponse response)

throws ServletException, IOException

{

response.setContentType(“text/html”);

PrintWriterout = response.getWriter();

StringdocType = “<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0Transitional//EN”>n”;

out.println(docType+

“<HTML>n”+

“<HEAD><TITLE>HelloServlet</TITLE></HEAD>n” +

“<BODYBGCOLOR=”#FDF5E6″>n” +

“<H1>Hellofrom your first servlet!</H1>n” +

“</BODY></HTML>n”);

}

} //end class

In order to build your servlet, you need to import a special Javalibrary file which contains all the class definitions which support the servletAPI. This library is part of theJava Enterprise Edition, and is not included with the Java Standard Edition.However, Apache Tomcat does include the needed Java library file. To add the servlet-api.jar file to yourproject, start by right clicking on your project and selecting Propertiesfrom the pop-up menu. You should see a display as shown below.

Select the Java Build Path on the left side of the display,and then select the Libraries tab. Click on the Add External JARs button. Thefollowing display should appear. Navigate to the following directory:

C:Program FilesApache SoftwareFoundationTomcat 5.5commonlib

Then select the servlet-api.jar file and click the Openbutton in the Jar Selection display. Finally, click the OK button in theProperties display shown above. Your project is now ready to successfully buildyour servlet project.

Once your project has been successfully built, use WindowsExplorer to copy the FirstServlet.class file from your project directory intothe Tomcat servlet directory given below.

C:Program FilesApache SoftwareFoundationTomcat 5.5webappsROOTWEB-INFclasses

Then start up Tomcat, and start a browser. Enter thefollowing URL into the browser.

http://localhost:8080/servlet/FirstServlet

You should see a greeting from the servlet in the browserwindow. Congratulations on successfully building your first servlet.

In the event that you are using Eclipse on a system otherthan the system running Tomcat, you will still need to get a copy of theservlet-api.jar file onto the system running Eclipse. The file is included inthis week’s material and can be downloaded from there.

Week 7 Project

The example servlets in the text and the lecture notesillustrate how to use HTTP session objects to keep track of session specificstate data. The servlet project for this week requires the use of HTTPsessions.

Your assignment is to implement a simple number guessinggame using HTML forms and a Java servlet. The HTML page should introduce thegame and provide a form for the user to guess a number on. The servlet shoulduse an HTTP session object to store all session specific data. If the sessionobject is empty, the servlet will need to generate initial values for allsession specific data items. Session specific data will include the number tobe guessed and the number of guesses so far.

The servlet should return an HTML form indicating whetherthe guess was high, low, or correct. The form should also indicate how manyguesses so far, and allow the user to enter another guess. If a user’s guess iscloser to the target than the user’s previous guess, set the background of theform to red. If a user’s guess is farther from the target than the user’sprevious guess, set the background of the form to blue. This will requireadditional session specific data to be stored.

When the user has guessed correctly, the servletshould reset all the session specific data, the form should have a backgroundcolor other than red or blue, and it should invite the user to start playing anew gameBuilding a Servlet Using Eclipse For the most part, building a servlet on Eclipse is done the same way as you would build any other Java class. Simply create a project for your class then add a class to your project. Place the servlet code into your class. As an example, add a class to your project called FirstServlet. Then copy the following code into your class. import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class FirstServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(“text/html”); PrintWriter out = response.getWriter(); String docType = “<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>n”; out.println(docType + “<HTML>n” + “<HEAD><TITLE>Hello Servlet</TITLE></HEAD>n” + “<BODY BGCOLOR=”#FDF5E6″>n” + “<H1>Hello from your first servlet!</H1>n” + “</BODY></HTML>n”); } } //end class In order to build your servlet, you need to import a special Java library file which contains all the class definitions which support the servlet API. This library is part of the Java Enterprise Edition, and is not included with the Java Standard Edition. However, Apache Tomcat does include the needed Java library file. To add the servlet-api.jar file to your project, start by right clicking on your project and selecting Properties from the pop-up menu. You should see a display as shown below. Select the Java Build Path on the left side of the display, and then select the Libraries tab. Click on the Add External JARs button. The following display should appear. Navigate to the following directory: C:Program FilesApache Software FoundationTomcat 5.5commonlib Then select the servlet-api.jar file and click the Open button in the Jar Selection display. Finally, click the OK button in the Properties display shown above. Your project is now ready to successfully build your servlet project. Once your project has been successfully built, use Windows Explorer to copy the FirstServlet.class file from your project directory into the Tomcat servlet directory given below. C:Program FilesApache Software FoundationTomcat 5.5webappsROOTWEB-INFclasses Then start up Tomcat, and start a browser. Enter the following URL into the browser. http://localhost:8080/servlet/FirstServlet You should see a greeting from the servlet in the browser window. Congratulations on successfully building your first servlet. In the event that you are using Eclipse on a system other than the system running Tomcat, you will still need to get a copy of the servlet-api.jar file onto the system running Eclipse. The file is included in this week’s material and can be downloaded from there. Week 7 Project The example servlets in the text and the lecture notes illustrate how to use HTTP session objects to keep track of session specific state data. The servlet project for this week requires the use of HTTP sessions. Your assignment is to implement a simple number guessing game using HTML forms and a Java servlet. The HTML page should introduce the game and provide a form for the user to guess a number on. The servlet should use an HTTP session object to store all session specific data. If the session object is empty, the servlet will need to generate initial values for all session specific data items. Session specific data will include the number to be guessed and the number of guesses so far. The servlet should return an HTML form indicating whether the guess was high, low, or correct. The form should also indicate how many guesses so far, and allow the user to enter another guess. If a user’s guess is closer to the target than the user’s previous guess, set the background of the form to red. If a user’s guess is farther from the target than the user’s previous guess, set the background of the form to blue. This will require additional session specific data to be stored. When the user has guessed correctly, the servlet should reset all the session specific data, the form should have a background color other than red or blue, and it should invite the user to start playing a new game