Java_实现用户登录和验证码

登录和验证码页面:login_session.jsp

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

    <script>
        window.onload = function () {
            document.getElementById("img").onclick=function () {
                this.src="/request/responseServlet5?time="+new Date().getTime();
            }
        }
    </script>
    <style>
        div{
            color:red;
        }
    </style>


</head>
<body>
    <form action="/request/login_session" method="post">
        <table>
            <tr>
                <td>用户名</td>
                <td><input type="text" name="username"> </td>
            </tr>

            <tr>
                <td>密码</td>
                <td><input type="password" name="password"> </td>
            </tr>
            <tr>
                <td>验证码</td>
                <td><input type="text" name="checkCode"> </td>
            </tr>
            <tr>
                <td colspan="2"><img id="img" src="/request/responseServlet5"> </td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" value="登录"> </td>
            </tr>

        </table>

    </form>
    <div><%=request.getAttribute("login_error") == null ? "":request.getAttribute("login_error") %></div>
    <div><%=request.getAttribute("check_error") == null ? "":request.getAttribute("check_error") %></div>

</body>
</html>

跳转成功页面:success.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>success</title>
</head>
<body>
    <h1><%=request.getSession().getAttribute("user")%>,欢迎您</h1>
</body>
</html>

后台:login_session
接收参数和session判断用户、密码、验证码正确与否

package cn.web.session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/login_session")
public class login_session extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码类型request
        request.setCharacterEncoding("utf-8");
        //2.接收到jsp的参数信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String checkCode = request.getParameter("checkCode");
        //3.获取生成的验证码信息
        HttpSession session = request.getSession();
        String checkcode_session = (String) session.getAttribute("checkcode_session");
        //由于session存储了验证码信息,所以当跳转后在浏览器点返回时候,不改变验证码又可以登录了.所以要在获取后立即删除
        session.removeAttribute("checkcode_session");
        //4.判断用户名或者验证码是否相同
        if (checkcode_session!=null && checkcode_session.equalsIgnoreCase(checkCode)){//equalsIgnoreCase是忽略大小写
            if ("zhangsan".equals(username) && "123".equals(password)){//这里可以添加数据库
                //登录成功
                session.setAttribute("user",username);//存储用户名
                //response重定向
                response.sendRedirect(request.getContextPath()+"/success.jsp");
            }else{
                //应为用户名或者密码登录失败
                request.setAttribute("login_error","用户名或者密码错误");
                //request转发到登录界面
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }
        }else{
            //验证码错误
            request.setAttribute("check_error","验证码错误");
            //转发到登录界面
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}

画验证码:responseServlet5

package cn.web.response;

import javax.imageio.ImageIO;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/responseServlet5")
public class responseServlet5 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//        response.setContentType("text/html;charset=utf-8");
        //字节输出流
//        ServletOutputStream out = response.getOutputStream();
        //输出数据
//        out.write("你好".getBytes("utf-8"));


        //如何制作验证码?

        //1.将图片存储到内存中
        int width = 100;//宽
        int height = 50;//高
        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);//宽、高、类型

        //2.美化图片
        //2.1填充
        Graphics g = image.getGraphics();//获得画笔对象
        g.setColor(Color.PINK);//画笔颜色:粉色
        g.fillRect(0,0,width,height);//填充矩形 从坐上角(0,0)点
        //2.2边框
        g.setColor(Color.BLUE);
        g.drawRect(0,0,width-1,height-1);//边框
        //2.3验证码字符
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        //2.4生成随机脚标
        Random rd = new Random();
        //获取一个session来存储验证码信息
        HttpSession session = request.getSession();

        //使用StringBuilder来存储验证码
        StringBuilder sb = new StringBuilder();

        for (int i =1 ;i<=4;i++){
            int index = rd.nextInt(str.length());//生成随机的脚标
            char ch = str.charAt(index);//根据脚标生成字符
            sb.append(ch);
            g.drawString(ch+"",width/5*i,height/2);
        }
        //将验证码信息存储到session中
        session.setAttribute("checkcode_session",sb.toString());

        //2.4画干扰线
        g.setColor(Color.GREEN);

        //随机生成坐标点

        for (int i = 0; i < 10; i++) {
            int x1 = rd.nextInt(width);
            int x2 = rd.nextInt(width);

            int y1 = rd.nextInt(height);
            int y2 = rd.nextInt(height);
            g.drawLine(x1,y1,x2,y2);
        }
        //3.在页面显示图片
        ImageIO.write(image,"jpg",response.getOutputStream());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}

结果:
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值