我写了一个登陆验证的小程序,用一个Filter来验证是否已经登陆,如果没有登陆,就直接转到登陆页面,但是现在是可以转到登陆页面,但是验证码不显示了
我把源码列出,请各位帮帮忙!!
用来生产验证码的Servlet
- Java code
package com.chk.cms.checkcode;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.io.PrintWriter;import java.util.Random;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;import com.sun.xml.internal.ws.api.ha.StickyFeature;public class CheckCodeServlet extends HttpServlet { // 定义验证码 图片的宽度 private int width; // 定义验证码图片的高度。 private int height; // 显示多少位字符 private int number; // 有哪些字符备选 private String codes; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("image/jepg"); // 创建一张图片 BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); // 设置背景色 g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); // 设置黑色边框 g.setColor(Color.BLACK); g.drawRect(0, 0, width - 1, height - 1); // 每个字符占据的宽度和高度 int x = (width - 1) / number; int y = height - 4; // 定义一个StringBuffer,用于存储code字符串,将来放置到session中 StringBuffer sb = new StringBuffer(); // 随机生成字符 Random random = new Random(); for (int i = 0; i < number; i++) { String code = String.valueOf(codes.charAt(random.nextInt(codes.length()))); // 随机生成颜色 int red = random.nextInt(255); int green = random.nextInt(255); int blue = random.nextInt(255); g.setColor(new Color(red, green, blue)); // 设置字体 g.setFont(new Font("宋体", Font.BOLD, 14)); g.drawString(code, i * x + 2, y); sb.append(code); } // 将字符串放置到Http Session中 request.getSession().setAttribute("codes", sb.toString()); // 随机生成一些干扰点 for (int i = 0; i < 50; i++) { int red = random.nextInt(255); int green = random.nextInt(255); int blue = random.nextInt(255); g.setColor(new Color(red, green, blue)); g.drawOval(random.nextInt(width), random.nextInt(height), 1, 1); } OutputStream out = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out); encoder.encode(image); out.flush(); out.close(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } public void init() throws ServletException { width = Integer.parseInt(this.getInitParameter("width")); height = Integer.parseInt(this.getInitParameter("height")); number = Integer.parseInt(this.getInitParameter("number")); codes = this.getInitParameter("codes"); }}
用于登陆的Servlet
- Java code
package com.chk.cms.checkcode;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); String checkcode = request.getParameter("checkcode");// 这个是用户输入的验证码 // 判断验证码是否正确 // 系统生成的验证码 String sessioncodes = (String) request.getSession().getAttribute("codes"); if (!sessioncodes.equalsIgnoreCase(checkcode)) { // 重定向到登录页面 // response.sendRedirect("index.jsp"); request.setAttribute("error", "验证码不正确"); request.getRequestDispatcher("/index.jsp").forward(request, response); return; } // 判断用户名是否存在,密码是否正确 Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cms", "root", "chengke168"); String sql = "select * from users where username = ?"; ps = conn.prepareStatement(sql); ps.setString(1, username); rs = ps.executeQuery(); if (rs.next()) { // 进入这一步,说明用户名已经存在 // 判断用户密码是否正确,如果不正确就给出提示“用户密码不正确” // 返回登陆页面 String pass = rs.getString("password");// 这里的password指的是数据库表中的字段名 if (!password.equals(pass)) { request.setAttribute("error", "用户 " + username + " 密码错误!"); request.getRequestDispatcher("/index.jsp").forward(request, response); return; } } else { // 说明用户名不存在,提示用户检查用户名,给出提示“用户名不存在” // 返回登陆页面 request.setAttribute("error", "用户 " + username + " 不存在!"); request.getRequestDispatcher("/index.jsp").forward(request, response); return; } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (rs != null) rs.close(); if (ps != null) ps.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } // 将登陆用户的名称保存在Session中,以便将来检查用户是否是正常登陆的用户 request.getSession().setAttribute("login_username", username); // 如果都通过了,就转向到主页面 request.getRequestDispatcher("/main.jsp").forward(request, response); }}