response重定向(登录)

常见场景:

  • 用户登录
 void sendRedirect(String var1) throws IOException;
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        /*  resp.setHeader("Location","/r/down");
            resp.setStatus(302);
        */
        resp.sendRedirect("/r/down");
    }
   protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String[] hobbys = req.getParameterValues("hobbys");
        resp.setCharacterEncoding("utf-8");
        System.out.println(username);
        System.out.println(password);
        System.out.println(Arrays.toString(hobbys));


        System.out.println("开始跳转");
/*
         ./:代表目前所在的目录。

         ../代表上一层目录。

        /:代表根目录*/
        resp.sendRedirect("./success.jsp");//转发和重定向路劲不一样,这里是重定向路径,也可以"success.jsp"
//        req.getRequestDispatcher("./success.jsp").forward(req,resp);
        System.out.println("\n跳转成功");
    }

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

登陆界面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<dvi>
    <form action="${pageContext.request.contextPath}/login" method="post">
        用户名:<input type="text" name="username"><br>
        密码:<input type="password" name="password"><br>
        爱好:
        <input type="checkbox" name="hobbys" value="520">520
        <input type="checkbox" name="hobbys" value="521">521
        <input type="checkbox" name="hobbys" value="522">522
        <input type="checkbox" name="hobbys" value="523">523
        <br>
        <input type="submit">
    </form>
</dvi>
</body>
</html>

posted @ 2022-07-29 14:33  我真的是小青蛙啊  阅读(109)  评论(0)    收藏  举报