servlet HttpServletRequest 类 2 获取请求参数

如何获取请求参数

<body>
  <form action="http://localhost:8080/07_servlet/parameterServlet" method="get">
    用户名:<input type="text" name="username"><br/>
    密码:<input type="password" name="password"><br/>
    兴趣爱好:<input type="checkbox" name="hobby" value="cpp">C++
    <input type="checkbox" name="hobby" value="java">Java
    <input type="checkbox" name="hobby" value="js">JavaScript<br/>
    <input type="submit">
  </form>
</body>
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        // 获取请求参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobby = req.getParameterValues("hobby");
        System.out.println("用户名:" + username);
        System.out.println("密码:" + password);
        System.out.println("兴趣爱好:" + Arrays.asList(hobby));
    }
上一篇:【从零开始学Servlet笔记】HttpServletRequest 和 HttpServletResponse


下一篇:servlet HttpServletRequest 类 3 中文字符编码问题