登录功能的开发(带验证码)

1.因为要和数据库进行连接,先获取数据源,创建连接对象。这些操作在工具类中完成。

public class DbUtil {
    private static DataSource ds=null;
    //创建连接池
    static{
        Properties p = new Properties();
        try {
            p.load(DbUtil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
           ds = BasicDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    //获取与数据库连接的对象
    public static Connection get_Connection(){
        Connection conn=null;
        try {
             conn = ds.getConnection();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return conn;
    }

    //关闭连接
    public static void close_Connection(Connection conn){
        try {
            if(conn!=null&&!conn.isClosed()){
                conn.close();
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

jdbc.properties文件:

driverClassName=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521/orcl
username=student
password=ok
initialSize=5
maxActive=10
maxWait=6000

2.开发dao层的代码:判断在前台输入的用户与数据库中的是否对应。

public boolean isExit(String username,String password){
        //获取连接
        Connection conn = DbUtil.get_Connection();
        try {
            //创建运载sql语句的对象
            PreparedStatement ps = conn.prepareStatement("select * from student where stu_name=? and password=?");
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            //如果在数据库中查到相应的记录,返回true
            while(rs.next()) return true;

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

3.最后开发请求到来时处理请求的servlet。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        //获取前台传过来的参数
        String username = request.getParameter("username");
        System.out.println(username);
        String password = request.getParameter("password");
        //验证用户是否存在
        LoginDao dao = new LoginDao();
        //如果存在,页面跳转到success.html
        if(dao.isExit(username, password)){
            response.sendRedirect("success.html");
        }
        else//否则跳转到fail.html
            response.sendRedirect("fail.html");

    }

4.验证码的生成。
首先,向服务器发送一个获取验证码的请求

<img id="check" alt="" src="checkcode">

新建一个Servlet处理这个请求,生成验证码,并返回给浏览器。

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Random r=new Random();
        //1.创建一个bufferedimage对象
        BufferedImage image=new BufferedImage(60, 20, BufferedImage.TYPE_INT_RGB);
        //2.绘制长方形
        Graphics g = image.getGraphics();
        g.setColor(new Color(233, 222, 220));
        g.fillRect(0, 0, 60, 20);
        //画干扰线 ganrao
        for(int i=0;i<100;i++){
            g.setColor(new Color(160+r.nextInt(30), 160+r.nextInt(30), 160+r.nextInt(30)));
            int x=r.nextInt(60);
            int y=r.nextInt(20);
            g.drawLine(x, y, x+r.nextInt(15), y+r.nextInt(15));
        }
        //写上四位随机数字
        g.setColor(new Color(22, 30, 44));
        int checkCode=r.nextInt(9000)+1000;
        g.drawString(checkCode+"", 10, 15);
        //构建输出流,将图片返回到浏览器
        OutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
    }

附加功能:看不清图片上的验证码,想要重新生成一个,用jQuery实现,重新发送一个请求。

$(function(){
        $("#change").click(function(){

            $("#check").attr("src",'checkcode?hhh='+Math.random());
        });
    })

猜你喜欢

转载自blog.csdn.net/u013739073/article/details/52347389
今日推荐