This laboratory exercise involves modifying your Counter servletso that you may set the value of the counter to an arbitrary value.The servlet takes its input from a HTML form, where the usercan enter a value, and the counter will be set to that value.
Level of Difficulty: 3 (requires some thought)
Estimated time: 20 minutes
Pre-requisites:
The goal of this exercise is to create a servlet that can both printout the value of a counter (as with the CounterServlet exercise), but also allows a user to set the counter to an arbitrary value.
Begin by creating a HTML form that you will use to view and setthe value of the counter. Remember this HTML file must go insideyour WAR file (the WebContent folder in Eclipse).
<form method="get" action="SettableCounter"><p> Set counter to equal: <input type="text" name="newValue" size="5"></p><p> <input type="submit" value="Set Counter"></p></form><p><a href="SettableCounter">Get counter value</a></p>
As you can see, your servlet code will need to be able to read thevalue of a parameter taken from a HTML form. Moreover, becausethe same servlet is being used for both viewing the counter valueand setting it, you will need to check if there is a form parametervalue supplied. If so, set the counter to the supplied value.If not, just display the counter.
Don't forget to set the Servlet mapping to /SettableCounter
Reading the value of form parameters in a servlet is quite easy.However before we get to that,first, notice the definition of the doGet()
that you have been using in earlier servlets:
protected void doGet (HttpServletRequest request, HttpServletResponse response) ...
In this definition, there is a "request" object (calledrequest
, although you may choose another name if you wish),and a "response" object (called response
).
The request Java object encapsulates all the details about the HTTPrequest that was sent by the user's web browser. This includes the URLof the request, the request method (e.g. GET or POST), the IP addressof the client's machine, etc. The request object has methods availableto retrieve these various pieces of information about the request.Another piece of information associated with the request object isthe names and values of any form parameters that the user might havesupplied. This is what we need for this exercise.
The response Java object encapsulates all the details of theHTTP response that will be sent back to the user's web browser.This includes the MIME type (content type) of the document, plusthe actual document itself. Even in the Hello World servlet, we have seen two examples of using the response object:
response.setContentType("text/html");
PrintWriter out = response.getWriter();
What happens when you change the content type to to be"text/html" instead?
You should also browse the , and get a feel for the methodsavailable on the HttpServletRequest
andHttpServletResposne
objects. Both are found in thepackage javax.servlet.http
.
By now we have established that to read HTML form parameters,we need to use a method on the request object.The method we need is called getParameter()
.
To read the value of a HTML form parameter named "newValue",use the following line of Java code:
String formValue = request.getParameter("newValue");
If there was no form field named "newValue", then thegetParameter()
function will return null
.To see whether a particular form field was supplied or not,you can just compare the result of getParameter()
against null
. [Hint: you need to do this as partof your SettableCounterServlet.]
Finally, note that when you read form parameters, they are always read as Java String objects.Your SettableCounterServlet will probably need to use the valueas a Java int, not a String.Here's a reminder of how to convert a String to an int, althoughin general you should be able to work things like this out foryourself (remember this is not an introductory Java course).Do not expect tutors to answer questions like this for you!
int formValueInt = Integer.parseInt(formValue);
(this may throw a NumberFormatException
thatyou must handle).
You should now have all the information you need to create yourSettableCounterServlet. You just have to put it all together!!!!
For debugging, sometimes it is useful to have your servletprint out a list of all form field names and their associatedvalues. Here is a piece of code to do that. Try it out inone of your own servlets if you wish, and use a HTML formwith several parameters to invoke your servlet.
Use the following code to create a JSP file and add to the WebContent folder in Eclipse.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><form method="get" action="parameters"> <div>First Text: <input type="text" name="textField" /></div> <div>Second Text: <input type="text" name="textField" /></div> <div>Third Text: <input type="text" name="textField" /></div> <div>Fourth Text: <input type="text" name="textField" /></div> <div>Fifth Text: <input type="text" name="textField" /></div> <br /> <div> <label for="first">First Choice</label> <input type="radio" name="radioButton" value="first" /> <label for="second">Second Choice</label> <input type="radio" name="radioButton" value="second" /> </div> <div> <label for="third">Third Choice</label> <input type="radio" name="radioButton" value="third" /> </div> <div> <input type="radio" name="radioButton" value="fourth" />Fourth Choice </div> <br /> <div> <input type="checkbox" name="checkBox" value="A" /> <label for="A">Choice A</label> </div> <div> <label for="B">Choice B</label> <input type="checkbox" name="checkBox" value="B" /> </div> <div> <input type="checkbox" name="checkBox" value="C" />C </div> <div> <input type="checkbox" name="checkBox" value="D" />D </div> <br /> <input type="submit" value="Submit" /></form></body></html>
The following Java code in the servlet prints out the header information of the request then all the parameter names and values passed from the JSP page above.