html:关于表单功能的学习

比如我在某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" ,然后就提示指定的资源不存在








原文地址:https://www.cnblogs.com/strinkbug/p/4952207.html