渚漪Day15——JavaWeb 05【HTTPServletRequest】

4、HTTPServletRequest

Servlet、用HTTPServletRequest接受并处理

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    req.setCharacterEncoding("utf-8");
    resp.setCharacterEncoding("utf-8");
    String username = req.getParameter("username");
    String password = req.getParameter("password");
    String[]hobbies = req.getParameterValues("hobbies");

    System.out.println(username+":"+password);

    System.out.println(Arrays.toString(hobbies));

    //resp.sendRedirect("success.jsp");
    req.getRequestDispatcher("success.jsp").forward(req, resp);
}

jsp、主页

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h1>登录</h1>
<div>
    <form action="${pageContext.request.contextPath}/login" method="post">
        用户名:<input type="text" name="username"><br>
        密&nbsp;&nbsp;&nbsp;码:<input type="password" name="password"><br>
        爱好:<input type="checkbox" name="hobbies" value="代码">代码
        <input type="checkbox" name="hobbies" value="vtb">vtb
        <input type="checkbox" name="hobbies" value="游戏">游戏
        <input type="checkbox" name="hobbies" value="音乐">音乐
        <input type="checkbox" name="hobbies" value="电影">电影
        <input type="checkbox" name="hobbies" value="运动">运动 <br>
        <input type="submit">
    </form>
</div>
</body>
</html>

jsp、成功

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>成功</title>
</head>
<body>
<h1>登录成功</h1>
</body>
</html>

猜你喜欢

转载自www.cnblogs.com/ijuysama/p/12785885.html