关于表单提交方式post和get区别

大家好,最近在学习孙老师的,在动手实验第9章会话跟踪中的程序的时候发现一个问题。
我将out.println("");
这行语句中的method改为get后,无论如何都取不到ac这个参数的值了,调试发现总是为null .
请问这是为什么呢?
[code="java"]
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    String username = request.getParameter("user");
    String password = request.getParameter("password");
    String action = request.getParameter("ac");
    if("chk".equals(action))
    {
        if("lwm".equals(username)&&"123456".equals(password))
        {
            StringBuilder sb = new StringBuilder();
            sb.append("username=") ;
            sb.append(username) ;
            sb.append("&password=") ;
            sb.append(password);
            Cookie cookie = new Cookie("userinfo",sb.toString());
            //设置cookie的最大生存时间,以秒为单位,如果要删除cookie,可以将时间设为0,如果时间值是负数,那么当客户端的
            //浏览器关闭的时候,cookie将被删除,只要设置的时间值是正数,Cookie将会被保存到客房机器上的硬盘中。
            cookie.setMaxAge(1800) ;
            response.addCookie(cookie);
            response.sendRedirect("greet2");
            return ;
        }
        else
        {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
            out.println("用户名或者密码错误,请<a href=login2>重新输入");
            out.close() ;
            return ;
        }

    }

    else
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<HTML>");
        out.println("  <HEAD><TITLE>登录页面</TITLE></HEAD>");
        out.println("  <BODY>");
        out.println("<form action=login2?ac=chk method=post>");
        out.println("<table>");
        out.println("<tr>");
        out.println("<td>请输入用户名</td>");
        out.println("<td><input type=text name=user></td>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<td>请输入密码</td>");
        out.println("<td><input type=password name=password></td>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<td><input type=submit value=提交></td>");
        out.println("<td><input type=reset value=重置></td>");
        out.println("</tr>");
        out.println("</table>");
        out.println("</form>");
        out.println("  </BODY>");
        out.println("</HTML>");
        out.flush();
        out.close();
    }
}

[/code]

[quote]
如果按你说的话,那么login2?ac=chk 就是url,对吧。
而 form data(即用户名和密码)是保存在request的message body中的。
那我在servlet里面又怎么得到了ac的值呢???也就是说ac属性的值既然不在form中,就不应该放在request 的message body中呀。。
[/quote]
使用 get 方法是, action的值只截取使用了 login2, ac=chk 被抛弃, 因为 get 的做法是将 form 的数据以
login2?user=xan&password=xxx
的形式编码到 url 中. 这里 ac=chk 已经被抛弃, 是不能通过 request 获取 ac 的值

使用 post 方法是, 不改变 action 的值, 而将 form data 编码到请求(泛指 HTTP 的请求)的 message body 中. 这里可以通过Servlet API request.getParameter("ac") 获取.

如果需要在使用 get 方法的同时能够传送"ac=chk" 到服务器, 可以在页面代码中添加 hidden field

如果你是这样写的 action=login2?ac=chk

那么无论你的 method=post 还是 method=get

ac都是以get方式提交的,

这个应该跟 post 和get 没有关系

或者你试一下不要写 method 属性, 应为默认的是 get提交

看看能否得到? :o

把HTML查看源代码发出来看看


当使用 get 方法时, 不能获取 ac 的值是正常行为, 因为 HTTP 协议规定, 但使用 get 时, 用户请求 url 的构成方式是:
action 名 + ? + form data, 如 login2?username=xan
[b]这里原来即使 action 里面有 ?ac=chk, 也会被忽略[/b]

而当使用 POST 方法时, 用户请求的 url 就是 action 属性的值, 而 form data 是放置在 request 的 message body 中传送到服务器的.

具体可以参考:
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.1
http://www.cs.tut.fi/~jkorpela/forms/methods.html