原创作品。转载请注明出处
很多情况下,需要传递一些信息,从浏览器到 Web 服务器,最终到后台程序。浏览器使用两种方法可将这些信息传递到 Web 服务器,分别为 GET 方法和 POST 方法。
GET 方法向页面请求发送已编码的用户信息。页面和已编码的信息中间用 ? 字符分隔,如下所示:
http://www.test.com/hello?key1=value1&key2=value2
这些信息使用 QUERY_STRING 头传递,并可以通过 QUERY_STRING 环境变量访问,Servlet 使用 doGet() 方法处理这种类型的请求。
另一个向后台程序传递信息的比较可靠的方法是 POST 方法。POST 方法打包信息的方式与 GET 方法基本相同,但是 POST 方法不是把信息作为 URL 中 ? 字符后的文本字符串进行发送,而是把这些信息作为一个单独的消息。消息以标准输出的形式传到后台程序,您可以解析和使用这些标准输出。Servlet 使用 doPost() 方法处理这种类型的请求。
Servlet 处理表单数据,这些数据会根据不同的情况使用不同的方法自动解析:
首先:Post传输数据时,不需要在URL中显示出来,而Get方法要在URL中显示。
其次:Post传输的数据量大,可以达到2M,而Get方法由于受到URL长度限制,只能传递大约1024字节.
再次:Post就是为了将数据传送到服务器段,Get就是为了从服务器段取得数据.而Get之所以也能传送数据,只是用来设计告诉服务器,你到底需要什么样的数据.Post的信息作为http请求的内容,而Get是在Http头部传输的。
六、再说Servlet中的GET和POST之间的区别
get和post这是http协议的两种方法,另外还有head, delete等 。这两种方法有本质的区别,get只有一个流,参数附加在url后,大小个数有严格限制且只能是字符串。post的参数是通过另外的流传递的,不通过url,所以可以很大,也可以传递二进制数据,如文件的上传。
在eclipse新建一个web工程,取名随便,然后添加web.xml index.jsp、还有servlet..结构如下:
GetServlet.java代码如下:
doGet
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name1 = request.getParameter("name1");
String price1 = request.getParameter("price1");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("<br></br>");
out.println("名称:"+name1);
out.println("<br></br>");
out.println("价格:"+price1);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
doPost
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name2 = request.getParameter("name2");
String price2 = request.getParameter("price2");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println("<br></br>");
out.println("名称:"+name2);
out.println("<br></br>");
out.println("价格:"+price2);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
index.jsp:
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Insert title here</title>
</head>
<body>
<form action="servlet/GetServlet" method="GET">
名称: <input type=text name=name1 value="">
价格: <input type=text name=price1 value=""><br>
<input type="submit" value="您采用了GET方式提交"> <br>
</form>
--------------------------------这是分割线-------------------------------------
<br></br>
<form action="servlet/GetServlet" method="POST">
名称: <input type=text name=name2 value="">
价格: <input type=text name=price2 value=""><br>
<input type="submit" value="您采用了POST方式提交">
</form>
</body>
</html>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置初始打开的页面 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<!-- 类名 -->
<servlet-name>GetServlet</servlet-name>
<!-- 所在的包 -->
<servlet-class>com.mucfc.chapter0.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetServlet</servlet-name>
<!-- 访问的网址 -->
<url-pattern>/servlet/GetServlet</url-pattern>
</servlet-mapping>
</web-app>
然后就是运行了:
用get提交
用post提交
因篇幅问题不能全部显示,请点此查看更多更全内容