Servlet表单

来源:互联网 发布:wings上央视知乎 编辑:程序博客网 时间:2024/06/06 02:20

很多情况下,需要传递信息,从浏览器到web服务器,最终到后台程序。浏览器使用两种方法可将这些信息传递到web服务器,分别为GET方法和POST方法。

GET方法

GET方法向页面请求发送已编码的用户信息。页面和已编码的信息中间用?字符分隔,如下所示:

http://www.test.com/hello?key1=value1&key2=value2

GET方法是默认的从浏览器向Web服务器传递信息的方法,它会产生一个很长的字符串,出现在浏览器的地址栏中。如果你要向服务器传递的是密码或其他敏感信息,请不要使用GET方法。GET方法有大小限制:请求字符串中最多只能有1024个字符。这些信息使用QUERY_STRING头传递,并通过QUERY_STRING环境变量访问,Servlet使用doGet()方法处理这种类型的请求。


POST方法

POST方法打包信息的方式与GET方法基本相同,但是POST方法不是把信息作为URL中?字符后的文本字符串进行发送,而是把这些信息作为一个单独的消息。消息以标准输出的形式传递到后台程序,你可以解析和使用这些标准输出。Servlet使用doPost()方法处理这种类型的请求。


使用Servlet读取表单数据

Servlet处理表单数据,这些数据会根据不同的情况使用不同的方法自动解析:

  • getParameter():你可以调用request.getParameter()方法来获取表单参数的值。
  • getParameterValues():如果参数出现一次以上,则调用该方法,并返回多个值,例如复选框。
  • getParameterNames():如果你想要得到当前请求中的所有参数的完整列表,则调用该方法。


使用URL的GET方法示例

下面是一个简单的URL,将使用GET方法向CustomerInfo.程序传递两个值。

http://localhost:8080/TomcatTest/CustomerInfo?name=user&password=123456

下面是处理Web浏览器输入的Customer.java Servlet程序。我们将使用getParameter()方法,可以很容易地访问传递的信息:

package com.JavaBean;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/**Servlet implementation class CustomerInfo**/@WebServlet("/CustomerInfo")public class CustomerInfo extends HttpServlet{private static final long serialVersionUID = 1L;/**@see HttpServlet#HttpServlet()**/public CustomerInfo(){super();// TODO Auto-generated constructor stub}/**@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)**/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{//设置响应内容类型response.setContentType("text/html;charset=UTF-8");PrintWriter out=response.getWriter();String title="使用GET方法读取数据";//处理中文String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");String docType="<!DOCTYPE html>\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>用户名</b>:"+name+"\n"+"<li><b>密码</b>:"+request.getParameter("password")+"\n"+"</ul>\n"+"</body></html>");}//处理POST请求的方法public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{doGet(request,response);}}

然后我们在web.xml文件中创建以下条目:

<?xml version="1.0" encoding="UTF-8"?><web-app>  <servlet>    <servlet-name>CustomerInfo1</servlet-name>//这个是你在html程序中为servlet-class起的别名    <servlet-class>com.JavaBean.CustomerInfo</servlet-class>//这个是servlet-class文件的路径,从/WEB-INF/classes开始访问  </servlet>  <servlet-mapping>    <servlet-name>CustomerInfo1</servlet-name>//同上述意思    <url-pattern>/TomcatTest/CustomerInfo</url-pattern>//对应到你在浏览器访问的html文件,这里是在ROOT/TomcatTest文件夹下  </servlet-mapping></web-app>

现在在浏览器地址栏输入http://localhost:8080/TomcatTest/Customer?name=user&password=123456,并在出发上述命令之前确保已经启动Tomcat服务器。运行结果如下:



使用表单的GET方法示例

下面是一个简单的实例,使用HTML表单和提交按钮传递两个值。我们将使用相同的Servlet CustomerInfo来处理输入。

<!DOCTYPE html><html><head><meta charset="gb2312"><title>使用HTML表单传递数据</title></head><body><form action="CustomerInfo" method="GET">用户名:<input type="text" name="name"><br />密码:<input type="text" name="password" /><input type="submit" value="提交" /></form></body></html>

将这个CustomerInfo.html文件和CustomerInfo.java文件放在同一个文件夹下。

尝试在地址栏输入localhost:8080/TomcatTest/CustomerInfo.html,会出现以下界面:

点击提交后,界面跳转为:


使用表单的POST方法示例

让我们对上面的Servlet做小小的修改,以便它可以处理GET和POST方法。下面的CustomerInfoo.java Servlet程序使用GET和POST方法处理由Web浏览器给出的输出。

注意:如果表单提交的数据中有中文数据则需要转码:

String name=new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");

package com.JavaBean;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class CustomerInfoo */@WebServlet("/CustomerInfoo")public class CustomerInfoo extends HttpServlet {private static final long serialVersionUID = 1L;           /**     * @see HttpServlet#HttpServlet()     */    public CustomerInfoo() {        super();        // TODO Auto-generated constructor stub    }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// 设置响应内容类型response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();String title = "使用 POST 方法读取表单数据";// 处理中文String name =new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8");String docType = "<!DOCTYPE html> \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>用户名</b>:"    + name + "\n" +    "  <li><b>密码</b>:"    + request.getParameter("password") + "\n" +    "</ul>\n" +    "</body></html>");}// 处理 POST 方法请求的方法public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet(request, response);}}

现在,编译部署上述的Servlet,修改web.xml文件同GET方法类似,并使用带有POST方法的CustomerInfoo.html进行测试,如下所示:

<!DOCTYPE html><html><head><meta charset="gb2312"><title>使用POST方法传递表单数据</title></head><body><form action="CustomerInfoo" method="POST">用户名:<input type="text" name="name"><br />密码:<input type="text" name="password" /><input type="submit" value="提交" /></form></body></html>

下面是表单的实际输出,输入用户名和密码,然后点击“提交”按钮,结果如下:


0 0