登录案例(JDBC+servlet)idea版

需求:
登录界面有用户名输入框,密码输入框,有验证码验证,用户名或密码错误与验证码错误有错误提示,登录成功跳转到成功界面,能获取到用户名

运行界面
登录案例(JDBC+servlet)idea版_第1张图片

包结构
登录案例(JDBC+servlet)idea版_第2张图片

验证码的代码

import javax.imageio.ImageIO;
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 java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

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

        int width=200;
        int height=50;
        BufferedImage bi=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

        //获取画笔

        Graphics g=bi.getGraphics();

        //设置颜色
        g.setColor(Color.orange);
        //填充背景色
        g.fillRect(0,0,width,height);

        //画边框
        g.setColor(Color.white);
        g.drawRect(0,0,width-1,height-1);

        //生成验证码
        Random random=new Random();
        String str="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        //用于存储验证码
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= 4 ; i++) {
            int num=random.nextInt(str.length());
            char ch=str.charAt(num);
            sb.append(ch);
            g.drawString(ch+"",width/5*i,height/2);

        }
        String session_checkCode = sb.toString();

        //将验证码存储到Session中
        request.getSession().setAttribute("session_checkCode",session_checkCode);

        //设置干扰线

        g.setColor(Color.GREEN);
        for (int i = 0; i < 10; i++) {

            int x1=random.nextInt(width);
            int x2=random.nextInt(width);
            int y1=random.nextInt(height);
            int y2=random.nextInt(height);
            g.drawLine(x1,x2,y1,y2);

        }

        ImageIO.write(bi,"jpg",response.getOutputStream());

    }

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

登录判断的代码

import shanghai.qf.dao.UserDao;
import shanghai.qf.dao.daoImple.UserDaoImpl;
import shanghai.qf.domain.User;

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("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }

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

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String checkCode = request.getParameter("checkCode");

        User user=new User();
        user.setUsername(username);
        user.setPassword(password);
        UserDao dao=new UserDaoImpl();
        User loginUser = dao.login(user);

        HttpSession session = request.getSession();
        String session_checkCode = (String)session.getAttribute("session_checkCode");

        session.removeAttribute("session_checkCode");

        if(session_checkCode!=null && session_checkCode.equalsIgnoreCase(checkCode)){

            if(loginUser != null){

                session.setAttribute("username",username);
                response.sendRedirect(request.getContextPath()+"/success.jsp"); //动态获取路径

            }

            else {

                request.setAttribute("login","用户名或密码错误");
                request.getRequestDispatcher("/index.jsp").forward(request,response);

            }


        }else{
            request.setAttribute("cc","验证码错误");
            request.getRequestDispatcher("/index.jsp").forward(request,response);

        }

    }
}

index.jsp代码部分

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

  
    登录页面

      

      

  

  
用户名
密码
验证码
<%=request.getAttribute("login")==null?"":request.getAttribute("login") %>
<%=request.getAttribute("cc")==null?"":request.getAttribute("cc") %>

success.jsp代码部分

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


    success



    

<%= request.getSession().getAttribute("username") %> 欢迎你

你可能感兴趣的:(java,servlet,JDBC)