form表单处理

表单数据处理通过request.getParameter()获取指定字段内容,通过request.getParameterValues()方式获取前端多个值以数组形式存储。
表单代码如下:



  
    用户注册
    
    
    
    
    
    

  
  
  
    
user name:
Password:
运动

servlet代码如下:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取用户输入表单
        String name = request.getParameter("username");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String[] hobby = request.getParameterValues("ah");
        String gw = request.getParameter("zw");
        String memo = request.getParameter("bz");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("");
        out.println("");
        out.println("  A Servlet Form");
        out.println("  ");
        out.print("    This is ");
        out.println(name);
        out.println(password);
        out.println(sex);
        for(String x:hobby){
            out.println(x);
        }
        out.println(gw);
        out.println(memo);
        out.print(this.getClass());
        out.println(", using the GET method");
        out.println("  ");
        out.println("");
        out.flush();
        out.close();
    }

可以将前台输入所有内容读入并进行处理。

你可能感兴趣的:(form表单处理)