session_login

login.jsp

<title>Insert title here</title>
<script type="text/javascript">

    function changeImage(){

        document.getElementById("myimage").src = "/d_session/checkimage?"+new Date().getTime();
    }
</script>
</head>
<body>
    <h3>XX网站的登录页面</h3>
    <font color="red" size="5">${message }</font>
    <form action="/d_session/login" method="post">
        用户名:<input type="text" name="username"><br/>
        密码:<input type="password" name="password"><br/>
        验证码:<input type="text" name="checkcode">
        <img src="/d_session/checkimage" style="cursor: pointer" onclick="changeImage();" id="myimage"><br/>
        <input type="submit" value="登录">
    </form>
</body>

CheckImageServlet doGet

public class CheckImageServlet extends HttpServlet {
    private final int WIDTH = 120;  
    private final int HEIGHT = 30;
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        BufferedImage bf = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = (Graphics2D) bf.getGraphics();//--Graphics2D is Graphics's child

        Color color = new Color(203, 222, 225);
        graphics.setColor(color);
        graphics.fillRect(0, 0, WIDTH, HEIGHT);

        String base ="ABCDEFGHIJKLMN";
        Random random = new Random();

        graphics.setColor(Color.RED);
        graphics.setFont(new Font("楷体",Font.BOLD,18));
        int m = 13;

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(base.length());
            char charAt = base.charAt(index);

            int jiaodu = random.nextInt(60)-30;

            double theta = jiaodu*Math.PI/180;

            graphics.rotate(theta,  m, 15);
            graphics.drawString(charAt+"", m, 19);
            sb.append(charAt);

            graphics.rotate(-theta,  m, 15);
            m+=20;
        }
    request.getSession().setAttribute("checkcode_session", sb.toString());

        graphics.setColor(Color.BLUE);
        for (int i = 0; i < 4; i++) {
            int x1 = random.nextInt(WIDTH);
            int x2 = random.nextInt(WIDTH);
            int y1 = random.nextInt(HEIGHT);
            int y2 = random.nextInt(HEIGHT);
            graphics.drawLine(x1, y1, x2, y2);
        }
        graphics.dispose();
        ImageIO.write(bf, "png", response.getOutputStream());
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

LoginServlet doGet

        request.setCharacterEncoding("UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String checkcode = request.getParameter("checkcode");

        String checkcode_session = (String) request.getSession().getAttribute("checkcode_session");
    if(checkcode==null||!checkcode.equalsIgnoreCase(checkcode_session)){
            request.setAttribute("message", "对不起, 您输入验证码信息不正确...");     request.getRequestDispatcher("/login.jsp").forward(request, response);
            return ;
        }
        // 做用户名和密码的校验
        // admin 
        if("admin".equals(username)&&"admin".equals(password)){
            request.getSession().setAttribute("loginUser", "admin");            response.sendRedirect(request.getContextPath()+"/index.jsp");   }else{
            request.setAttribute("message", "对不起, 您 是用户名或密码不正确...");
request.getRequestDispatcher("/login.jsp").forward(request, response);  
        }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值