Request应用

前端的页面

<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>登陆</h1>

<div style="text-align: center">
    <form action="/login" method="post">
        用户名:<input type="text" name="username"> <br>
        密码:<input type="password" name="password"> <br>
        爱好:
        <input type="checkbox" name="hobbys" vaule="女孩">女孩
        <input type="checkbox" name="hobbys" vaule="代码">代码
        <input type="checkbox" name="hobbys" vaule="唱歌">唱歌
        <input type="checkbox" name="hobbys" vaule="电影">电影
        <input type="checkbox" name="hobbys" vaule="蜡笔小新">蜡笔小新

        <br>
        <input type="submit">
    </form>
</div>>


</body>
</html>

前端为post的提交方式,后台也要用post方法

 @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        //得到参数username的内容
        String username = req.getParameter("username");
        //得到password的参数
        String password = req.getParameter("password");
        //得到hobbys的参数
        String[] hobbys = req.getParameterValues("hobbys");
        System.out.println("===========");
        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbys));
        System.out.println("============");
        //跳转语句
        req.getRequestDispatcher(req.getContextPath() + "success.jsp").forward(req,resp);
    }
}

跳转的前台页面

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

posted @ 2022-07-25 16:47  笑到肚子疼  阅读(15)  评论(0)    收藏  举报