METHOD属性
METHOD属性是一个必须的属性,它指定Web浏览器将表单数据传送到服务器过程中所用的方法。在传送表单数据上有两种方法可供选择:POST和GET。
POST方法会使Web浏览器分两步传送数据。浏览器首先尝试与在ACTION属性中指定的服务器进行连接,当连接完成,再将表单数据通过这个特定传输通道(separate transmission)传送给服务器。服务器要以标准方式读取表单的参数。
GET方法会使Web浏览器连接服务器并通过这个单一传输通道传送表单数据。浏览器将会把表单数据追加到ACTION所指定的URL之后(就像命令行参数一样)并用问号将URL与参数值分隔开。
你要用哪一种方法呢?这里有一个指定方针:
·如果表单只有少量的输入域,那么就使用GET方法。由于所有的数据都是通过单独传输方式发送的,这样也就可以得到很高的效率。
·由于一些服务器限制命令行参数的长度(这是GET方法的工作方式),对于很大的表单或是含有超长字符串值参数的表单,我们将只能选用POST方法。
·如果你很注重安全问题,那么就使用POST方法。由于GET方法把表单数据就像命令行参数一样加在URL后传送,因此通过网络探测器或是分析服务器的记录文件都可以很容易地获取所传数据的具体值。而对于POST方法来说,数据将通过特定传输通道传送。
传送附加参数
你可以把附加参数非常方便地通过ACTION属性所指定的统一资源地址传送出去。你只要对附加变量进行编码并把人他们按命令行参数的方式添加上去。让我们举个例子,如果你有两个参数名为“a”“b”,你可以按照“application/x-www-form-urlencoded”方法(参看表8.2)将这些参数编码如下:
a=3&b=24
下面是一个输入邮箱地址,年龄,姓名等提交单的输入界面
HTML代码:
<html>
<head>
<title>Customer Survey</title>
</head>
<body>
<h1><center>Customer Survey</center></h1>
<hr><br>
<form method=POST action="http://localhost:9090/examples/servlets/servlet/EchoSurvey">
<table border=0>
<tr>
<td align=right>Name:</td>
<td colspan=2 align=left><input type=text name=name size=40></td>
</tr>
<tr>
<td align=right>Email Address:</td>
<td colspan=2 align=left><input type=text name=email size=40></td>
</tr>
<tr valign=top>
<td align=right>Age:</td>
<td align=left>
<input type=radio name=age value="<18">Less than 18<br>
<input type=radio name=age value="18-25">18-25
</td>
<td align=left>
<input type=radio name=age value="26-40">26-40<br>
<input type=radio name=age value=">40">Over 40
</td>
</tr>
<tr valign=top>
<td align=right>Operation System:</td>
<td align=left>
<select name=os size=.5 multiple>
<option>Win/95
<option>NT
<option>Solaris
<option>HP-UX
<option>Other
</select>
</td>
</tr>
<tr>
<td></td>
<td><input type=checkbox name=more value="yes">
Send me more information
</td>
</tr>
<tr>
<td align=right>Comments:</td>
<td colspan=2 align=left>
<textarea name=comments cols=40 rows=4>
</textarea>
</td>
</tr>
<tr>
<td></td>
<td>
<input type=reset value="Clear Form">
<input type=submit value="Submit">
</td>
</tr>
</html>
提交的EchoSurvey.java代码为
1 import javax.servlet.*; 2 import javax.servlet.http.*; 3 4 /** 5 * <p>This is a simple servlet that will echo survey information 6 * that was entered into an HTML form. 7 */ 8 9 public class EchoSurvey extends HttpServlet 10 { 11 /** 12 * <p>Performs the HTTP POST operation 13 * 14 * @param req The request from the client 15 * @param resp The response from the servlet 16 */ 17 18 public void doPost(HttpServletRequest req, 19 HttpServletResponse resp) throws ServletException, java.io.IOException 20 { 21 // Set the content type of the response 22 resp.setContentType("text/html"); 23 24 // Create a PrintWriter to write the response 25 java.io.PrintWriter out = 26 new java.io.PrintWriter(resp.getOutputStream()); 27 28 // Print a standard header 29 out.println("<html>"); 30 out.println("<head>"); 31 out.println("<title>Survey Complete</title>"); 32 out.println("</head>"); 33 out.println("<body>"); 34 out.println("<h1><center>Your survey has been processed!"); 35 out.println("</center></h1><hr><br>"); 36 out.println("Values were:"); 37 out.println("<dir>"); 38 39 // Get the name 40 String name = req.getParameter("name"); 41 out.println("Name=" + name + "<br>"); 42 43 // Get the email address 44 String email = req.getParameter("email"); 45 out.println("Email=" + email + "<br>"); 46 47 // Get the age 48 String age = req.getParameter("age"); 49 out.println("Age=" + age + "<br>"); 50 51 // Get the operating system. There could be more than one 52 // value 53 String values[] = req.getParameterValues("os"); 54 out.print("Operating Systems="); 55 if (values != null) { 56 for (int i = 0; i < values.length; i++) { 57 if (i > 0) out.print(", "); 58 out.print(values[i]); 59 } 60 } 61 out.println("<br>"); 62 63 // Get the 'more information' flag 64 String more = req.getParameter("more"); 65 out.println("More information=" + more + "<br>"); 66 67 // Get the comments 68 String comments = req.getParameter("comments"); 69 out.println("Comments:<br>"); 70 out.println("<dir>"); 71 72 // Comment lines are separated by a carriage return/line feed 73 // pair - convert them to an HTML line break <br> 74 out.println(toHTML(comments)); 75 out.println("</dir>"); 76 77 out.println("</dir>"); 78 79 // Wrap up 80 out.println("</body>"); 81 out.println("</html>"); 82 out.flush(); 83 } 84 85 /** 86 * <p>Initialize the servlet. This is called once when the 87 * servlet is loaded. It is guaranteed to complete before any 88 * requests are made to the servlet 89 * 90 * @param cfg Servlet configuration information 91 */ 92 93 public void init(ServletConfig cfg) throws ServletException 94 { 95 super.init(cfg); 96 } 97 98 /** 99 * <p>Destroy the servlet. This is called once when the servlet 100 * is unloaded. 101 */ 102 103 public void destroy() 104 { 105 super.destroy(); 106 } 107 108 /** 109 * <p>Convert any carriage return/line feed pairs into 110 * an HTML line break command (<br>) 111 * 112 * @param line Line to convert 113 * @return line converted line 114 */ 115 private String toHTML(String line) 116 { 117 String s = ""; 118 119 if (line == null) { 120 return null; 121 } 122 123 // Cache the length of the line 124 int lineLen = line.length(); 125 126 // Our current position in the source line 127 int curPos = 0; 128 129 // Loop through the line and find all of the carriage 130 // return characters (0x0D). If found, convert it into 131 // an HTML line break command (<br>). If the following 132 // character is a line feed (0x0A) throw it away 133 while (true) { 134 135 // Make sure we don't run off the end of the line 136 if (curPos >= lineLen) { 137 curPos = 0; 138 break; 139 } 140 141 int index = line.indexOf(0x0D, curPos); 142 143 // No more characters found 144 if (index == -1) { 145 break; 146 } 147 148 // Add any data preceding the carriage return 149 if (index > curPos) { 150 s += line.substring(curPos, index); 151 } 152 153 // Add the line break command 154 s += "<br>"; 155 156 // Adjust our position 157 curPos = index + 1; 158 159 // If the next character is a line feed, skip it 160 if (curPos < lineLen) { 161 if (line.charAt(curPos) == 0x0A) { 162 curPos++; 163 } 164 } 165 } 166 167 // Be sure to add anything after the last carriage return 168 // found 169 if (curPos > 0) { 170 s += line.substring(curPos); 171 } 172 return s; 173 } 174 }
输入完数据,单击submit按钮,显示的页面为: