您的位置:首页 > 其它

表单中post与get的区别

2008-11-14 13:01 288 查看
解决思路:
两者的区别需要通过提交表单后才看得出来,主要是在数据发送方式和接收方式上。
具体步骤:
Post和Get都是表单属性Method的可选值,Method的默认值为Get,两者的主要区别在于:
1.在客户端,Get方式在通过URL提交数据,提交后在地址栏中的地址如图1.4.3所示。



图1.4.3 Get方式提交表单后的地址栏

而Post提交后地栏不变,如图1.4.4所示。



图1.4.4 Post方式提交表单后的地址栏不变

2.在服务器端只能用Request.QueryString来获取Get方式提交来的数据,用Post方式提交的数据只能用Request.Form来获取:

<%@language="VBScript" Codepage="936"%>
<html>
<head>
<title> 表单提交方式测试</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form method="post" action="<%=Request.ServerVariables("Script_Name")%>">
提交数据:
<input name="oStr" type="text">
<br>
提交方式:
<select name="select" onchange="this.form.method=this.value">
<option value="Post" selected>Post</option>
<option value="Get">Get</option>
</select>
<br>
<input type="submit" name="Submit" value="提交">
</form>
<%
if Request("Submit")<>"" then
Response.Write "通过"&Request.ServerVariables("Request_Method")&"方式提交的数据为:"
if Request.ServerVariables("Request_Method")="GET" then
Response.Write Request.QueryString("oStr")
else
Response.Write Request.Form("oStr")
end if
end if
%>
</body>
</html>

注意:虽然两种提交方式可以统一用Request("oStr")来获取提交数据,但是这样对程序效率有影响,不推荐使用。
特别提示
通过IIS运行本例代码(用http://localhost/method.asp这种方式来浏览,有关IIS的安装和配置,请参考第四部分),输入所提交的数据,选择Post方式提交,将看到图1.4.4所示的效果。选择Get方式提交,效果将如图1.4.3所示。

特别说明

一般来说,尽量避免使用Get方式提交表单,因为有可能会导致安全问题。比如说在登陆表单中用Get方式,用户输入的用户名和密码将在地址栏中暴露无遗。但是在分页程序中,用Get方式就比用Post好。本例中用到的表单的属性解释(ASP部分请参考第四部分):
Get把参数添加到action属性指定的地址中,并以锚方式打开。
Post通过HTTP post处理发送数据。
------------------------------------------------------------------------------------------
在前面已经讲过Servlet通过下面的方法来提供服务:
  实现service方法。

  实现HttpServlet的doMethod方法(doGet、doDelete、doOptions、 doPost、doPut、doTrace)。

  通常,service方法用来从客户请求(request)中提取信息,访问扩展资源,并基于上面的信息提供响应(response)。

  对于HTTP
Servlets,正确提供响应的过程是首先填写响应(response)的头信息,然后从响应(response)中得到输出流,最后向输出流中写入内
容信息。响应(response)头信息必须最先设置。下面将描述如何从请求(request)中获得信息和产生HTTP响应(response)。

  取得客户端请求

  一个HttpServletRequest对象提供到达HTTP
头部数据,也允许你获取客户端的数据。怎样获取这些数据取决于HTTP端请求方法。不管用任何HTTP方式,你都可以用
getParameterValues方法返回特定名称的参数值。对于用 HTTP GET
请求的方式,这个getQueryString方法将会返回一个可以用来分析的值。

  客户端请求(request)包含了从客户端传递到Servlet的数据。所有的请求(request)都实现了ServletRequest接口。这个接口定义了一些方法访问下面的信息,如表14-1所示。

  表14-1 ServletRequest接口方法

  ?型 描 述 对 应 方 法

  参数,用来在客户端和Servlet之间传送信息 getAttribute(String name)

   getAttributeNames()

   getInputStream()

   getParameter(String name)

  getParameterMap()

  getParameterNames()

  getParameterValues(String name)

  对象值属性,用来在Servlet容器和Servlet

  之间,或者协作的Servlet之间传递信息 removeAttribute(String name)

   setAttribute(String name, Object o)

  有关请求使用的协议信息,

  客户端和服务器在请求中的调用 getContentLength()

   getContentType()

   getProtocol()

  getReader()

  getRealPath(String path)

  getRemoteAddr()

  getRemoteHost()

   getRequestDispatcher(String path)

  有关请求使用的协议信息,

  客户端和服务器在请求中的调用 getScheme()

   getServerName()

   getServerPort()

   isSecure()

  有关localization的信息 getCharacterEncoding()

   getLocale()

   getLocales()

   setCharacterEncoding(String env)

  下面的代码段示范了如何使用request中的方法获得客户端信息。

  Enumeration params = request.getParameterNames();

  String paramName = null;

  String[] paramValues = null;

  

  while (params.hasMoreElements()) {

  paramName = (String) params.nextElement();

  paramValues = request.getParameterValues(paramName);

  System.out.println(" Parameter name is " + paramName);

  for (int i = 0; i < p>

   System.out.println(", value " + i + " is " + paramValues[i].toString());

  }

  

  HTTP Servlets使用HTTP request对象(HttpServletRequest),它包含了request URL、HTTP头信息、查询字符串,等等。HTTP request URL 包括几个部分:

  http://:?

  一般情况下:

  requestURI = contextPath + servletPath + pathInfo

  Context path:通过getContextPath方法获得。

  Servlet Path:通过getServletPath方法获得。

  PathInfo:通过getPathInfo方法获得。

    如表14-2所示。

  表14-2 路径的对应

  Request Path Path Elements

  /catalog/help/feedback.jsp ContextPath: /catalog ServletPath:

   /help/feedback.jsp PathInfo: null

  提供HTTP响应

  响应(response)包含了在服务器和客户端之间传递的数据。所有的响应(response)都实现了ServletResponse接口。这个接口定义了一些方法提供给开发人员使用,如表14-3所示。

  表14-3 ServletResponse接口方法

  ?型 描 述 对 应 方 法

  获得向客户端发送数据的输出流 发送字符流:getWriter()

   发送字节流:getOutputStream()

  指示响应返回的内容类型(例如:text/html)

  已经注册的内容类型名称保存在IANA

  (Internet Assigned Numbers Authority) setContentType(java.lang.String type)

  指出是否是缓冲输出。默认情况下写入输出的

  内容被立即发送到客户端。使用缓冲后写入输出的内容先

  不发送到客户端,这样Servlet有更多的时间设置相应的

  状态码和头信息,或者转移到其他的Web资源 flushBuffer()

   getBufferSize()

   isCommitted()

   reset()

   resetBuffer()

   setBufferSize(int size)

   setContentLength(int len)

  设置localization信息 getCharacterEncoding()

   getLocale()

   setLocale(java.util.Locale loc)

  HTTP response类(HttpServletResponse)有一些代表HTTP头信息的域:

  

  状态码用来指出响应(response)失败的原因。

  Cookies在客户端存储应用相关的信息,有时cookies用来维护和标识用户的session。

  Servlet首先设置响应(response)头信息,包括响应(response)的内容类别和缓冲区大小,然后在doGet方法中从响应
(response)获得PrintWriter
,最后向输出中写入HTML代码,调用close()方法提交这次对客户端的响应(response)。示范代码如下:

  public void doGet (HttpServletRequest request,

  HttpServletResponse response)

  throws ServletException, IOException

  

  // 设置头信息

  response.setContentType("text/html");

  response.setBufferSize(8192);

  PrintWriter out = response.getWriter();

    

  // 向response中输出

  out.println("" +

   "");

  ...

  out.println("");

  // 关闭输出流

  out.close();

}
-----------------------------------------------------------------------------------------
深入研究表单提交方式:GET/POST

Hackfan

本文平台:Windows 2000 Professional + Apache 1.3.17 + Perl 5.6.1 + Internet Explorer 5.00.2920.0000

  大家知道目前表单提交的方式有GET和POST。我在这里不多说什么,给大家看一个以GET方式提交的表单的请求:

GET /cgi-bin/tech/method.cgi?GET=GET HTTP/1.1
Accept: image/gif,
image/x-xbitmap, image/jpeg, image/pjpeg,
application/vnd.ms-powerpoint, application/vnd.ms-excel,
application/msword, */*
Referer: http://localhost//other.html
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
Host: localhost:8080
Connection: Keep-Alive

  这个请求是我们通过这个HTML代码发出的:

<form action="http://localhost:8080/cgi-bin/tech/method.cgi" method="GET">
<input type="text" size="10" value="GET" name="GET">
<input type=submit value="GET方式">
</form>

  这个请求已经超出了我们研究的范围,我们只研究其中的第一行。其中,第一个"GET"说出了提交的方式,是以GET方式提交的;中间的就是提交
给服务器上哪个程序,前面一部分"/cgi-bin/tech/method.cgi"就是我们HTML的form中action的内容,而后面
的"GET=GET"就是HTML的form中,input的内容:我们发现IE已经把这个表单的内容转换成特定格式了。在Perl中,通
过$GET=$ENV{'QUERY_STRING'}获得以GET发送的数据。

  我们再看一个以POST方式提交的表单的请求:

POST /cgi-bin/tech/method.cgi HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-
powerpoint, application/vnd.ms-excel, application/msword, */*
Referer: http://localhost//other.html
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
Host: localhost:8080
Content-Length: 9
Connection: Keep-Alive

POST=POST

  同样给出HTML:

<form action="http://localhost:8080/cgi-bin/tech/method.cgi" method="POST">
<input type="text" size="10" value="POST" name="POST">
<input type=submit value="POST方式">
</form>

  我们发现其中的数据跑到了最下面。在Perl中,通过read(STDIN,$POST,$ENV{'CONTENT_LENGTH'})获得以POST发送的数据。我记得GET发送数据最多只能1024字节,而POST好像很大很大!

  思考:如果我有这么一段HTML代码,它将会出现什么问题呢?

<form action="http://localhost:8080/cgi-bin/tech/method.cgi?GET=GET" method="POST">
<input type="text" size="10" value="POST" name="POST">
<input type=submit value="GET/POST方式">
</form>

  这个代码在很多程序上可能用到过,但是大多数人不会好好的想一想,究竟哪些内容是以GET发送的,哪些内容是以POST发送的。我们看看它的请求是什么:

POST /cgi-bin/tech/method.cgi?GET=GET HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-
powerpoint, application/vnd.ms-excel, application/msword, */*
Referer: http://localhost//other.html
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
Host: localhost:8080
Content-Length: 9
Connection: Keep-Alive

POST=POST

  哈!原来是以POST发送的。但是,你一定发现了有一部分数据放在了第一行,就是和GET的情况一样的。其实这个例子很典型,是POST和GET混发!
  不相信你在Perl中,用read(STDIN,$POST,$ENV{'CONTENT_LENGTH'})和$GET=$ENV{'QUERY_STRING'}看看,到底哪个里面有"GET=GET"这个数据。

  我给大家提供设备,大家自己去研究研究:

HTML部分:  

<html>
<head>
<title>Get-Post</title>
</head>

<body>
<form action="/cgi-bin/tech/method.cgi" method="GET">
<input type="text" size="10" value="GET" name="GET">
<input type=submit value="GET方式">
</form>
<form action="/cgi-bin/tech/method.cgi" method="POST">
<input type="text" size="10" value="POST" name="POST">
<input type=submit value="POST方式">
</form>
<form action="/cgi-bin/tech/method.cgi?GET=GET" method="POST">
<input type="text" size="10" value="POST" name="POST">
<input type=submit value="GET/POST方式">
</form>
<form action="/cgi-bin/tech/method.cgi?name=Hackfan&age=16&email=hackfan@163.net" method="POST">
<input type="text" size="10" value="Suzhou" name="address">
<input type="text" size="10" value="msger.net" name="homepage">
<input type="text" size="10" value="106814" name="qq">
<input type=submit value="复杂GET/POST方式">
</form>
</body>
</html>

Perl部分:

#!c:/perl/bin/perl.exe

$|=1;

print "Content-type:text/html/n/n";

print "发送方式:$ENV{'REQUEST_METHOD'}<br>/n";
if(read(STDIN,$POST,$ENV{'CONTENT_LENGTH'})){
print "POST得到的数据:$POST<br>/n";
}
if($GET=$ENV{'QUERY_STRING'}){
print "GET得到的数据:$GET<br>/n";
}

$METHOD="POST";

for($i=0;$i<=1;$i++){
foreach(split(/&/,$$METHOD)){
$_=~s//+//g;
($name,$value)=split(/=/,$_);
$name=~s/%([a-fA-f0-9][a-fA-f0-9])/pack("C",hex($1))/eg;
$value=~s/%([a-fA-f0-9][a-fA-f0-9])/pack("C",hex($1))/eg;
$$METHOD{$name}=$value;
}
$METHOD="GET";
}

$METHOD="POST";

for($i=0;$i<=1;$i++){
print "Hash形式的$METHOD数据遍历:<br>/n";
foreach(keys %{$METHOD}){
print "/$".$METHOD."{".$_."}=$$METHOD{$_}<br>/n";
}
print "<br>/n";
$METHOD="GET";
}

exit;

####代码结束####

  好了,我要说的是,搞这个研究究竟有什么意义呢?
  意义是:让你知道,用户提交的数据哪些是用POST方式,哪些使用GET方式的!
  其实我上面那段Perl代码已经包括了很多的技术。你通过阅读就可以知道%GET里面放的是用GET方式提交的,%POST同理!

  如果你对我编写的Perl代码感兴趣,欢迎切磋:QQ:106814。至于我如何获得IE发送来的请求的,我要说我是用Perl编的一个
Server监听8080端口,我是不是像欧姆一样搞研究大多东西都自己编写(当然,让我编写一个操作系统就有点难度了,不过WebServer凑合)?
开玩笑呢!

QQ:106814
Email:hackfan@163.com
Personal Page:http://www.msger.net/hackfan

作者Blog:http://blog.csdn.net/hackfan/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: