星期三
package com.itheima.web; 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.io.IOException; @WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); // 1.获取参数 String user=request.getParameter("username"); String password=request.getParameter("password"); String inputCode=request.getParameter("inputCode"); // 2.获取生成的验证码 String login_code=(String) request.getSession().getAttribute("check_code"); request.getSession().removeAttribute("check_code"); // 3.比较验证码 if(login_code.equalsIgnoreCase(inputCode)==true){ //验证码正确 //比较用户名是否正确 if(user.equals("123456")&&password.equals("123456")){ //用户信息正确 //存储信息,用户信息 request.getSession().setAttribute("name",user); //用于测试的输出语句 System.out.println("1111111111111111"); //response.sendRedirect(request.getContextPath()+"/success.jsp"); request.getRequestDispatcher("success.jsp").forward(request,response); } else { //用户信息错误 //用于测试的输出语句 //System.out.println("22222222222"); request.setAttribute("login_error","用户名或密码错误"); request.getRequestDispatcher("login.jsp").forward(request,response); } } else { //验证码错误 //用于测试的输出语句 System.out.println("33333333333"); request.setAttribute("cc_error","验证码错误"); request.getRequestDispatcher("login.jsp").forward(request,response); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
这是登录的servlet类
package com.itheima.web; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.OutputStream; import java.util.Random; 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; @WebServlet("/checkCodeServlet") public class CheckCodeServlet extends HttpServlet { //产生随机的字体 private Font getFont(){ //创建random对象,用于生成随机数 Random random = new Random(); //创建字体数组,用于封装不同字体的Font对象 Font font[] = new Font[5]; font[0] = new Font("Ravie", Font.PLAIN, 24); font[1] = new Font("Antique Olive Compact", Font.PLAIN, 24); font[2] = new Font("Forte", Font.PLAIN, 24); font[3] = new Font("Wide Latin", Font.PLAIN, 24); font[4] = new Font("Gill Sans Ultra Bold", Font.PLAIN, 24); return font[random.nextInt(5)]; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jpeg"); OutputStream os = response.getOutputStream(); int width = 166, height = 30; //建立指定宽、高和BufferedImage对象 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR); Graphics g = image.getGraphics();//将画笔绘制于image上 Color c= g.getColor();//保存当前画笔的颜色 g.fillRect(0, 0, width, height);//填充矩形 char[] ch = "abcdefghigklmnopqrstuvwxyz2345678901".toCharArray();//随机产生字符串 int length = ch.length;//随机字符串长度 String sRand = "";//保存随机产生的字符串 Random random = new Random(); for (int i=0; i<8; i++){ g.setFont(getFont());//设置字体 String rand = new Character(ch[random.nextInt(length)]).toString();//随机生成0~9的数字 sRand += rand; //设置随机颜色 g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255))); g.drawString(rand, 20*i+6, 25); } request.getSession().setAttribute("check_code",sRand); //保存生成的验证码到session中 //产生随机干扰点 for(int i=0; i<20; i++){ int x1 = random.nextInt(width); int y1 = random.nextInt(height); g.drawOval(x1, y1, 2, 2); } g.setColor(c);//重置画笔颜色 g.dispose();//释放此图形的上下文及使用的所有系统资源 request.getSession().setAttribute("check_code", sRand);//验证码记录到session ImageIO.write(image, "JPEG", os);//输出图像到页面 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); } }
这是生成验证码图片的servlet类