首页 > Web开发 > 详细

html:关于表单功能的学习

时间:2015-11-10 11:55:15      阅读:284      评论:0      收藏:0      [点我收藏+]
比如我在某jsp页面中写了如下表单:
<form action="/MavenWeb/TestFormPost" method="get">
  <p>First name: <input type="text" name="fname" /></p>
  <p>Last name: <input type="text" name="lname" /></p>
  <input type="submit" value="Submit" />
</form> 

在TestFomPost这个servlet中写如下代码:
    protected void doGet(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Hello servlet </title>");
        out.println("</head>");
        out.println("<body>");
        Map<String, String[]> paramMap = request.getParameterMap();
        Set<Entry<String, String[]>> entrySet = paramMap.entrySet();
        for(Entry<String, String[]> entryentrySet)
        {
            out.println(String.format("<p> key=%s, value = %s </p>"entry.getKey(), entry.getValue()[0]));
        }
        out.println("</body>");
        out.println("</html>");
        out.close();
    }  

 在运行的时候其实会发现,TestFormPost 的url后面会带上刚刚那个表单的所有字段的名称和值
例如:
这里遇到一个坑,在表单的action中指定路径的时候,比如是相对于localhost:8080之后的相对路径,而不是当前虚拟目录之后的相对路径。
所以一开始我只写了action="/TestFormPost" ,然后就提示指定的资源不存在








html:关于表单功能的学习

原文:http://www.cnblogs.com/strinkbug/p/4952207.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!