第五次JSP作业

login.jsp:

<
script type="text/javascript"> function _change(){ /* 1.得到img元素 2.修改其src为/SessionDemo/VeifyCodeServlet */ var imgEle = document.getElementById("img"); var v = new Date; var s = v.getTime(); //imgEle.src = "/SessionDemo/VeifyCodeServlet?a=" + new Date().getTime(); imgEle.src = "/SessionDemo/VeifyCodeServlet?a=" + s; } function _check(){ if(form1.username.value==""){ alert("用户名不能为空"); return; } if(form1.password.value==""){ alert("密码不能为空"); return; } if(form1.vCode.value==""){ alert("验证码不能为空"); return; } } </script> </head> <body> <h1>登录</h1> <% /* 读取名为uname的cookie,如果为空显示"",不为空显示cookie的值 */ String uname = ""; Cookie[] cs = request.getCookies(); // 获取请求中所有的cookie if(cs != null){ // cookie不为空 for(Cookie c : cs){ // 循环遍历Cookie[] if("uname".equals(c.getName())){ // 查找名称为uname的cookie uname = c.getValue(); // 将值赋给uname变量 } } } %> <% // 显示错误信息 String s = ""; String msg = (String) request.getAttribute("msg"); if (msg != null) { s = msg; } %> <%-- 本页面提供登录表单,还要显示错误信息 --%> <font color="red"><b><%=s%></b> </font> <form action="/SessionDemo/LoginServlet" method="post" name="form1"> 用户名:<input type="text" name="username" value="<%=uname%>" /><br />&nbsp&nbsp&nbsp码:<input type="password" name="password" /><br /> 验证码:<input type="text" name="vCode" size="3"/> <img id="img" src="/SessionDemo/VeifyCodeServlet"> <a href="javascript: _change()">看不请,换一张</a> <br /> <input type="submit" value="登录" onclick="_check()"/> </form> </body>

成功页面:

<%
    String username = (String)session.getAttribute("username");
    if(username==null){
        /*
         *    向request域中保存错误信息,并转发到login.jsp 
         */
        request.setAttribute("msg", "你还没有登录");
        request.getRequestDispatcher("/session2/login.jsp").forward(request, response);
        return;
    }
%>

    <h1>succ1</h1>
    欢迎<%=username %>访问本页面。
    <a href="session2/exit.jsp">退出</a>

退出(exit)页面:

<body>
    <%
        session.removeAttribute("username");
        response.sendRedirect("login.jsp");
    %>
</body>

LoginServlet:

public class LoginServlet extends HttpServlet {

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        /*
         * 验证码处理 1.得到session里的验证码文本 2.获取到文本框中输入的文本 3.比较
         */
        String sessiontext = (String) request.getSession().getAttribute(
                "session_vcode");
        String wtext = request.getParameter("vCode");
        if (!wtext.equalsIgnoreCase(sessiontext)) {
            request.setAttribute("msg", "输入的验证码有误");
            request.getRequestDispatcher("/session2/login.jsp").forward(
                    request, response);
            return;
        }

        /*
         * 1、获取表单信息
         */
        // 中文处理
        request.setCharacterEncoding("utf-8");
        // 获取
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        /*
         * 2、校验用户名和密码是否正确
         */
        if (!"itcast".equalsIgnoreCase(username)) {
            // 成功
            /*
             * 附加项:把用户名保存到cookie中,发送给客户端浏览器
             * 当再次打开login.jsp时,login.jsp会读取request中的cookie,把它显示到用户名文本框中
             */

            Cookie cookie = new Cookie("uname", username);// 创建cookie
            cookie.setMaxAge(60 * 60 * 24);// 设置cookie名长为一天
            response.addCookie(cookie);// 保存cookie

            /*
             * 3、如果成功 > 保存用户信息到session中 > 重定向到succ1.jsp
             */
            HttpSession session = request.getSession();// 获取session对象
            session.setAttribute("username", username);// 向session域中保存数据
            response.sendRedirect("/SessionDemo/session2/succ1.jsp");
        } else {
            // 失败
            /*
             * 4、如果失败 > 保存错误数据到request域中 >
             * 转发到login.jsp(如果使用重定向request域中的数据就会因为第二次请求 而获取不到)
             */
            request.setAttribute("msg", "用户名或密码错误");
            RequestDispatcher rd = request
                    .getRequestDispatcher("/session2/login.jsp");
            rd.forward(request, response);// 转发
        }
    }
}

VeifyCodeServlet:

public class VeifyCodeServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*
         * 1.生成图片
         * 2.保存图片上的文本到session中
         * 3.把图片响应给客户端
         */
        VerifyCode vc = new VerifyCode();
        BufferedImage bi = vc.getImage();
        request.getSession().setAttribute("session_vcode", vc.getText());
        VerifyCode.output(bi, response.getOutputStream());
    }

}

截图:

 

 

 

 

 

posted @ 2020-04-14 21:08  破男孩儿  阅读(146)  评论(0编辑  收藏  举报