Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Advanced Java Programming (Java EE) - Servlets - Lab exercise - Form parameters 31242/32549/ Java EEAdvanced Internet Programming INFO: What's New FAQ Software Machines Oracle Assignments MODULES: 00 Admin 01 Intro 02 Architecture 03 Servlets 04 JSP 05 JDBC 06 RMI 06 JNDI 07 EJB 08 XML 08 Web Services 09 Security 10 Transactions 11 Legacy 12 Review 99 Design LINKS: START page Faculty of IT UTS Module: Servlets Lab exercise - Form parameters This laboratory exercise involves modifying your Counter servlet so that you may set the value of the counter to an arbitrary value. The servlet takes its input from a HTML form, where the user can 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: Completed the Counter servlet lab exercise A SettableCounter servlet The goal of this exercise is to create a servlet that can both print out 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 set the value of the counter. Remember this HTML file must go inside your WAR file (the WebContent folder in Eclipse).

Set counter to equal:

Get counter value

As you can see, your servlet code will need to be able to read the value of a parameter taken from a HTML form. Moreover, because the same servlet is being used for both viewing the counter value and setting it, you will need to check if there is a form parameter value 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 Request and Response objects 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 (called request, 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 HTTP request that was sent by the user's web browser. This includes the URL of the request, the request method (e.g. GET or POST), the IP address of the client's machine, etc. The request object has methods available to retrieve these various pieces of information about the request. Another piece of information associated with the request object is the names and values of any form parameters that the user might have supplied. This is what we need for this exercise. The response Java object encapsulates all the details of the HTTP response that will be sent back to the user's web browser. This includes the MIME type (content type) of the document, plus the 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 JavaDoc documentation for servlets, and get a feel for the methods available on the HttpServletRequest and HttpServletResposne objects. Both are found in the package javax.servlet.http. Reading form parameters 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 the getParameter() 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 part of 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 value as a Java int, not a String. Here's a reminder of how to convert a String to an int, although in general you should be able to work things like this out for yourself (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 that you must handle). You should now have all the information you need to create your SettableCounterServlet. You just have to put it all together!!!! Aside: printing all form parameters For debugging, sometimes it is useful to have your servlet print out a list of all form field names and their associated values. Here is a piece of code to do that. Try it out in one of your own servlets if you wish, and use a HTML form with 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"%> Insert title here
First Text:
Second Text:
Third Text:
Fourth Text:
Fifth Text:

Fourth Choice

C
D

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. // don't forget to import the java.util.Enumeration class. import java.util.Enumeration; /* Printing header information */ out.println("### Header Information ###"); @SuppressWarnings("unchecked") Enumeration e = request.getHeaderNames(); while (e.hasMoreElements()) { String name = e.nextElement(); String value = request.getHeader(name); out.println(name + ": " + value); } out.println("========================================\n"); /* Printing all the parameter names and values */ out.println("### Parameter Names and Values ###"); @SuppressWarnings("unchecked") Enumeration parameterNames = request.getParameterNames(); while (parameterNames.hasMoreElements()) { String name = parameterNames.nextElement(); String[] paramValues = request.getParameterValues(name); for (String value : paramValues) { out.println("\"" + name + "\"=\"" + value + "\""); } out.println("---------------------------------------"); } © 2008 University of Technology, Sydney. All Rights Reserved. Redistribution without permission prohibited.