Order Now

Building a Servlet Using Eclipse.

Building 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.

 

Implementation

  • The HTML page should have a form for the user to guess a number.
  • The servlet should use an HTTP session object to store all session specific data.
  • The servlet should return an HTML form indicating whether the guess was high, low, or correct.
  • When the user has guessed correctly, the servlet should reset all the session, and the user can start playing a new game.

Click here to open the 

Building a Servlet Using Eclipse.

 

Order Now

Calculate Your Order Price


Price (USD)
$