<< Chapter < Page | Chapter >> Page > |
Listing 16 - The servlet program named Java4550a.java.
/*File Java4550a.java, Copyright 1999, R.G.Baldwin
Rev 01/04/14The purpose of this program is to illustrate session
tracking through the use of hidden fields.Each time the submit button is pressed, this servlet
creates a web page and sends it back to the browser.The new web page contains the historical data for the
session in hidden fields in the web page.The following is a typical web page after the names
Tom, Dick, and Harry have been entered.:The program also detects the name of the server that it
is running on and uses that name to construct the actionelement of a form. In the output shown below, the
servlet was running on a computer named dell8700.<html><head><title>Java4550a</title></head><body><form method="get" action="http://dell8700:8080/Java4550a"><p>Enter a name and press the button</p><p>Name:<input type="text" name="firstName"/></p><input type="submit" value="Submit Name"/><p>Your list of names is:<br/>Tom<br/><input type="hidden" name="item" value="Tom">Dick<br/><input type="hidden" name="item" value="Dick">Harry<br/><input type="hidden" name="item" value="Harry"></form></body></html>**********************************************************/
import java.io.*;import javax.servlet.*;
import javax.servlet.http.*;import java.net.*;
public class Java4550a extends HttpServlet{public void doGet(HttpServletRequest req,
HttpServletResponse res)throws ServletException, IOException{
//Get and display name of localhostInetAddress inetAddress = InetAddress.getLocalHost();
String hostName = inetAddress.getHostName();
System.out.println(hostName);//An array for getting and saving the values contained
// in the hidden fields named item.String[] items = req.getParameterValues("item");//Get the submitted name for the current GET request
String name = req.getParameter("firstName");//Establish the type of output
res.setContentType("text/html");//Get an output streamPrintWriter out = res.getWriter();//Construct an html form and send it back to the client
out.println("<html>");
out.println("<head><title>Java4550a</title></head>");
out.println("<body>");out.println("<form method='get' action="
+ "\"http://" + hostName + ":8080/Java4550a\">");
out.println("<p>Enter a name and press the button</p>");
out.println("<p>Name:<input type=\"text\" name="
+ "\"firstName\"/></p>");
out.println("<input type=\"submit\" value="
+ "\"Submit Name\"/>");
out.println("<p>Your list of names is:<br/>");
if(name == null){out.println("Empty</p>");
}//end ifif(items != null){for(int i = 0; i<items.length; i++){
//Display names previously saved in hidden fieldsout.println(items[i] + "<br/>");
//Save the names in hidden fields on form currently// under construction.
out.println("<input type=\"hidden\" name=\"item\" "
+ "value=\"" + items[i]+ "\">");
}//end for loop}//end if
if(name != null){//Display name submitted with current GET request
out.println(name + "<br/>");
//Save name submitted with current GET request in a// hidden field on the form currently under
// constructionout.println("<input type=\"hidden\" name=\"item\" "
+ "value=\"" + name + "\">");
}//end ifout.println("</form></body></html>");}//end doGet()
}//end class Java4550a
Listing 17 - The JSP program named Java4550a.jsp. |
---|
<html><head><title>Java4550a</title></head><body><form method="get" action="http://localhost:8080/Java4550a.jsp"><p>Enter a name and press the button</p><p>Name:<input type="text" name="firstName"/></p><input type="submit" value="Submit Name"/><p>Your list of names is:<br/><%
String[]items = request.getParameterValues("item");
response.setContentType("text/html");String name = request.getParameter("firstName");
if(name == null){out.println("Empty");
}//end ifif(items != null){
for(int i = 0; i<items.length; i++){
%><%= items[i] %><br/><input type="hidden" name="item" value="<%=items[i]%>"/><%
}//end for loop}//end if
if(name != null){%><%= name %><br/><input type="hidden" name="item" value="<%=name%>"/><%
}//end if%></p></form></body></html> |
-end-
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?