当您需要将某些信息从浏览器传递到Web服务器并最终传递到后端程序时,您必定会遇到很多情况.浏览器使用两种方法将此信息传递给Web服务器.这些方法是GET方法和POST方法.
GET方法发送附加到页面请求的编码用户信息.页面和编码信息由?(问号)符号分隔如下 :
http://www.test.com/hello?key1 = value1& key2 = value2
GET方法是将信息从浏览器传递到Web服务器的默认方法它会生成一个长字符串,显示在浏览器的位置:框中.如果您有密码或其他敏感信息要传递到服务器,请勿使用GET方法. GET方法有大小限制:请求字符串中只能使用1024个字符.
此信息使用QUERY_STRING标头传递,可通过QUERY_STRING环境变量访问,Servlet处理此类型的请求使用 doGet()方法.
将信息传递给后端程序的通常更可靠的方法是POST方法.这以与GET方法完全相同的方式打包信息,而不是在a之后将其作为文本字符串发送? (问号)在URL中将其作为单独的消息发送.此消息以标准输入的形式提供给后端程序,您可以解析并用于处理. Servlet使用 doPost()方法处理此类请求.
Servlet处理表单数据根据情况和减号使用以下方法自动解析;
getParameter() : 您调用request.getParameter()方法来获取表单参数的值.
getParameterValues() : 如果参数出现多次并且返回多个值,请调用此方法,例如复选框.
getParameterNames() : 如果您想要当前请求中所有参数的完整列表,请调用此方法.
这是一个简单的URL,它将使用GET方法将两个值传递给HelloForm程序.
http://localhost:8080/HelloForm?first_name = ZARA& last_name = ALI
以下是用于处理Web浏览器给出的输入的 HelloForm.java servlet程序.我们将使用 getParameter()方法,这样可以非常轻松地访问传递的信息 :
// Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // Extend HttpServlet class public class HelloForm extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Using GET Method to Read Form Data"; String docType = "<!doctype html public -//w3c//dtd html 4.0 " + "transitional//en>\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor = #f0f0f0>\n" + "<h1 align = center>" + title + "</h1>\n" + "<ul>\n" + " <li><b>First Name</b>: " + request.getParameter("first_name") + "\n" + " <li><b>Last Name</b>: " + request.getParameter("last_name") + "\n" + "</ul>\n" + "</body>" + "</html>" ); } }
假设您的环境设置正确,请编译HelloForm.java,如下所示 :
$ javac HelloForm.java
如果一切顺利,上面的编译会产生 HelloForm.class 文件.接下来,您必须在< Tomcat-installationdirectory>/webapps/ROOT/WEB-INF/classes中复制此类文件,并在< Tomcat-installation-中的 web.xml 文件中创建以下条目:目录>/webapps/ROOT/WEB-INF/
<servlet> <servlet-name>HelloForm</servlet-name> <servlet-class>HelloForm</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloForm</servlet-name> <url-pattern>/HelloForm</url-pattern> </servlet-mapping>
现在在浏览器的位置:框中输入 http://localhost:8080/HelloForm?first_name = ZARA&last_name = ALI 确保你已经启动了tomcat服务器,然后在浏览器中触发上面的命令.这将生成以下结果 :
Using GET Method to Read Form DataFirst Name: ZARALast Name: ALI
这是一个简单的例子它使用HTML FORM和提交按钮传递两个值.我们将使用相同的Servlet HelloForm来处理此输入.
<html> <body> <form action = "HelloForm" method = "GET"> First Name: <input type = "text" name = "first_name"> <br /> Last Name: <input type = "text" name = "last_name" /> <input type = "submit" value = "Submit" /> </form> </body> </html>
将此HTML保存在文件Hello.htm中,并将其放在< Tomcat-installationdirectory>/webapps/ROOT目录中.当您访问 http://localhost:8080/Hello.htm 时,以下是上述表格的实际输出.
尝试输入名字和姓氏,然后单击"提交"按钮以在运行tomcat的本地计算机上查看结果.根据提供的输入,它将生成与上例中提到的类似的结果.
让我们做一点修改在上面的servlet中,它可以处理GET以及POST方法.下面是 HelloForm.java servlet程序,用于处理Web浏览器使用GET或POST方法给出的输入.
// Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // Extend HttpServlet class public class HelloForm extends HttpServlet { // Method to handle GET method request. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Using GET Method to Read Form Data"; String docType = "<!doctype html public -//w3c//dtd html 4.0 " + "transitional//en>\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor = #f0f0f0>\n" + "<h1 align = center>" + title + "</h1>\n" + "<ul>\n" + " <li><b>First Name</b>: " + request.getParameter("first_name") + "\n" + " <li><b>Last Name</b>: " + request.getParameter("last_name") + "\n" + "</ul>\n" + "</body>" "</html>" ); } // Method to handle POST method request. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
现在编译并部署上面的Servlet并使用Hello方法使用POST方法测试它,如下所示 :
<html> <body> <form action = "HelloForm" method = "POST"> First Name: <input type = "text" name = "first_name"> <br /> Last Name: <input type = "text" name = "last_name" /> <input type = "submit" value = "Submit" /> </form> </body> </html>
这是上面表格的实际输出,尝试输入名字和姓氏,然后点击提交按钮,在本地机器上看到tomcat的结果正在运行.
根据提供的输入,它将生成与上述示例中提到的类似的结果.
当需要选择多个选项时,使用复选框.
以下是示例HTML代码CheckBox.htm,用于带有两个复选框的表单
<html> <body> <form action = "CheckBox" method = "POST" target = "_blank"> <input type = "checkbox" name = "maths" checked = "checked" /> Maths <input type = "checkbox" name = "physics" /> Physics <input type = "checkbox" name = "chemistry" checked = "checked" /> Chemistry <input type = "submit" value = "Select Subject" /> </form> </body> </html>
此代码的结果如下表格
下面给出的是CheckBox.java servlet用于处理Web浏览器为复选框按钮提供的输入的程序.
// Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // Extend HttpServlet class public class CheckBox extends HttpServlet { // Method to handle GET method request. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Reading Checkbox Data"; String docType = "<!doctype html public -//w3c//dtd html 4.0 " + "transitional//en>\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor = #f0f0f0>\n" + "<h1 align = center>" + title + "</h1>\n" + "<ul>\n" + " <li><b>Maths Flag : </b>: " + request.getParameter("maths") + "\n" + " <li><b>Physics Flag: </b>: " + request.getParameter("physics") + "\n" + " <li><b>Chemistry Flag: </b>: " + request.getParameter("chemistry") + "\n" + "</ul>\n" + "</body>" "</html>" ); } // Method to handle POST method request. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
对于上面的例子,它会显示以下结果 :
Reading Checkbox DataMaths Flag : : onPhysics Flag: : nullChemistry Flag: : on
以下是使用HttpServletRequest的 getParameterNames()方法读取所有可用表单参数的通用示例.此方法返回一个Enumeration,其中包含未指定顺序的参数名称
一旦我们有了Enumeration,我们就可以使用 hasMoreElements()
// Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; // Extend HttpServlet class public class ReadParams extends HttpServlet { // Method to handle GET method request. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Reading All Form Parameters"; String docType = "<!doctype html public -//w3c//dtd html 4.0 " + "transitional//en>\n"; out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n" + "<body bgcolor = #f0f0f0>\n" + "<h1 align = center>" + title + "</h1>\n" + "<table width = 100% border = 1 align = center>\n" + "<tr bgcolor = #949494>\n" + "<th>Param Name</th>" "<th>Param Value(s)</th>\n"+ "</tr>\n" ); Enumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String)paramNames.nextElement(); out.print("<tr><td>" + paramName + "</td>\n<td>"); String[] paramValues = request.getParameterValues(paramName); // Read single valued data if (paramValues.length == 1) { String paramValue = paramValues[0]; if (paramValue.length() == 0) out.println("<i>No Value</i>"); else out.println(paramValue); } else { // Read multiple valued data out.println("<ul>"); for(int i = 0; i < paramValues.length; i++) { out.println("<li>" + paramValues[i]); } out.println("</ul>"); } } out.println("</tr>\n</table>\n</body></html>"); } // Method to handle POST method request. public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
现在,尝试使用以下形式的上述servlet :
<html> <body> <form action = "ReadParams" method = "POST" target = "_blank"> <input type = "checkbox" name = "maths" checked = "checked" /> Maths <input type = "checkbox" name = "physics" /> Physics <input type = "checkbox" name = "chemistry" checked = "checked" /> Chem <input type = "submit" value = "Select Subject" /> </form> </body> </html>
现在使用上面的表格调用servlet会产生以下结果 :
Reading All Form ParametersParam NameParam Value(s)mathsonchemistryon
您可以尝试上面的servlet来读取任何其他表单的数据,包括文本框,单选按钮或下拉框等其他对象.