Request应用

获取前端数据

请求转发

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>

<%--${pageContext.request.contextPath}获取当前项目路径,需要导入jsp servlet的依赖包--%>
<form action="/s3/req" method="post">
    用户名:<input name="username" type="text"><br>
    密 码:<input name="password" type="password"><br>
    爱好:
    <input type="checkbox" name="hobbys" value="泡妞">泡妞
    <input type="checkbox" name="hobbys" value="K歌">K歌
    <input type="checkbox" name="hobbys" value="赛车">赛车

    <input type="submit">
</form>
</body>
</html>
public class Req extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String name = req.getParameter("username");
        String pwd = req.getParameter("password");
        System.out.println(name+":"+pwd);
        String[] hobbys = req.getParameterValues("hobbys");
        System.out.println(Arrays.toString(hobbys));

        req.getRequestDispatcher("login.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);

    }
}

3.注册servlet

posted @ 2022-01-23 17:55  ︶ㄣ演戲ㄣ  阅读(10)  评论(0)    收藏  举报  来源