java实现验证码的步骤:
1.创建验证码的实体类,代码如下:
- import java.awt.BasicStroke;
- 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.util.Random;
- import javax.imageio.ImageIO;
- public class VerifyCode {
- /**
- *楠岃瘉鐮?
- * @author 瑕冧匠鍗?
- */
- private int w = 50;
- private int h = 25;
- private Random r = new Random();
- private String[] fontNames = {"瀹嬩綋","鍗庢枃妤蜂綋","榛戜綋","寰蒋闆呴粦","妤蜂綋_GB2312"};
- private String codes = "23456789abcdefghjkmnpqrstuvwxyzABCEFGHIJKLMNPQRSTUVWXYZ";
- private Color bgColor = new Color(255,255,255);
- private String text;
- private Color randomColor(){
- int red = r.nextInt(200);
- int green = r.nextInt(200);
- int blue = r.nextInt(200);
- return new Color(red,green,blue);
- }
- private Font randomFont(){
- int index = r.nextInt(fontNames.length);
- String fontName = fontNames[index];
- int style = r.nextInt(4);
- int size = r.nextInt(6)+15;
- return new Font(fontName,style,size);
- }
- private void drawLine(BufferedImage image){
- int num = 3;
- Graphics2D g2 = (Graphics2D)image.getGraphics();
- for(int i=0;i<num;i++){
- int x1 = r.nextInt();
- int y1 = r.nextInt();
- int x2 = r.nextInt();
- int y2 = r.nextInt();
- g2.setStroke(new BasicStroke(1.5F));
- g2.setColor(Color.BLUE);
- g2.drawLine(x1,y1,x2,y2);
- }
- }
- private char randomChar(){
- int index = r.nextInt(codes.length());
- return codes.charAt(index);
- }
- private BufferedImage createImage(){
- BufferedImage image = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
- Graphics2D g2 = (Graphics2D)image.getGraphics();
- g2.setColor(this.bgColor);
- g2.fillRect(0, 0, w, h);
- return image;
- }
- public BufferedImage getImage(){
- BufferedImage image = createImage();
- Graphics2D g2 = (Graphics2D)image.getGraphics();
- StringBuilder sb = new StringBuilder();
- for(int i= 0;i<4;i++){
- String s = randomChar()+"";
- sb.append(s);
- float x = i*1.0F*w/4;
- g2.setFont(randomFont());
- g2.setColor(randomColor());
- g2.drawString(s, x, h-5);
- }
- this.text = sb.toString();
- drawLine(image);
- return image;
- }
- public String getText(){
- return text;
- }
- public static void output (BufferedImage image,OutputStream out) throws IOException{
- ImageIO.write(image, "JPEG", out);
- }
- }
2.创建验证码的servlet
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- import java.io.PrintWriter;
- 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 util.VerifyCode;
- @WebServlet("/VerifyCodeServlet")
- public class VerifyCodeServlet extends HttpServlet {
- /**
- * Servlet
- * @author weiqiang
- */
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- VerifyCode vc = new VerifyCode();
- response.setHeader("Cache-Control", "no-cache");
- response.setContentType("image/jpeg");
- BufferedImage bim = vc.getImage();
- ImageIO.write(bim, "JPEG", response.getOutputStream());
- String verifycode = vc.getText();
- request.getSession().setAttribute("verifyCode", verifycode);
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doGet(request, response);
- }
- public void destroy(){
- super.destroy();
- }
- public void init() throws ServletException{
- }
- }
- 3.在xml中进行配置
- servlet>
<servlet-name>VerifyCodeServlet</servlet-name>
<servlet-class>cn.com.shxt.servlet.VerifyCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>VerifyCodeServlet</servlet-name>
<url-pattern>/VerifyCodeServlet</url-pattern>
</servlet-mapping>
3.在需要使用验证码的地方使用如下代码:
[html] view plain copy
- <!--这是看不清刷新一下的函数-->
- <script>
- function changeVerifyCode(){
- document.getElementById("validateCodeImg").src="VerifyCodeServlet?t="+Math.random();
- }
- </script>
- <!-- 这是输入验证码的输入框-->
- <INPUT id=txtSN size=4 name=txtSN>
- <!-- 这是显示的验证码,以图片显示-->
- <!-- src的路劲为servlet的路劲 -->
- <img src="<%=basePath%>VerifyCodeServlet" border=0 id="validateCodeImg" style="height: 25px; width: 57px"/><a href="#" onclick="changeVerifyCode()">看不清?换一张</a></TD>
4.实际效果如下
String code = request.getParameter("code");
String verifyCode = (String) request.getSession().getAttribute("verifyCode");
else if(!code.equals(verifyCode)){//登陆失败
request.setAttribute("message","验证码错误");
request.getRequestDispatcher("/login.jsp").forward(request, response);
前台的code和session中的验证码进行比较,实现验证码的功能。
时间: 2024-10-27 17:47:11