网页开发学习笔记——实现可刷新验证码

实现可刷新验证码思路

在HTML或jsp页面中使用img标签,调用创建的验证码Servlet生成相应,将生成的验证码图片返回到客户端上。

具体代码

newjsp.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<form action="MyServlet" enctype ="multipart/form-data"  method = "post">
<img src = "CheckCode" alt = "验证码" onclick = "self.location.reload();" style = "cursor:pointer" 
alt = "如果看不清验证码,请点图片刷新" align = "middle"/> <br/>
</form>
</body>
</html>
  • img标签中的src属性为我们用于生成验证码的Servlet类名;
  • onclick属性调用了location.reload()函数,实现每次点击时重新访问页面来刷新验证码;
  • style属性设置为当游标放在图片上时变为可点击状态。

CheckCode.java

package Servlet;

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 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 javax.servlet.http.HttpSession;


/**
* Servlet implementation class CheckCode
*/
@WebServlet(name="CheckCode",urlPatterns = {"/CheckCode"})
public class CheckCode extends HttpServlet {
   private static final long serialVersionUID = 1L;
      
   /**
    * @see HttpServlet#HttpServlet()
    */
   public CheckCode() {
       super();
   }
   

   /**
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   //需要修改默认response类型为图片类型
   	response.setContentType("image/jpeg");
   	OutputStream out = response.getOutputStream();
   	try {
   		response.setHeader("Pragma","no-cache");
   		response.setHeader("Cache-Control","no-cache");
   		//设置为永不过期
   		response.setDateHeader("Expires", 0);
   		//先把图片画到内存的图片对象里
   		BufferedImage image = new BufferedImage(50,18,BufferedImage.TYPE_INT_RGB);
   		Graphics g = image.getGraphics();
   		g.setColor(Color.LIGHT_GRAY);
   		g.fillRect(0, 0, 50,18);
   		g.setColor(Color.LIGHT_GRAY);
   		g.drawRect(0, 0, 50, 18);
   		//验证码包含的字符
   		String str = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";
   		String code = "";
   		//从34个字符中随机选4个作为验证码
   		for(int i = 0;i<4;i++) {
   			int k = (int)(Math.random()*34);
   			char c = str.charAt(k);
   			code += c;
   		}
   		//把画出来的字符先存到Session里面(否则提交后会失效),验证的时候可以调用
   		HttpSession session = request.getSession();
   		session.setAttribute("code", code);
   		g.setColor(Color.BLACK);
   		Font font = new Font("DIALOG",Font.ITALIC,15);
   		g.setFont(font);
   		g.drawString(code, 3, 15);
   		g.dispose();
   		ImageIO.write(image,"JPEG",out);
   		//提交缓冲区数据
   		out.flush();
   		}finally {
   			out.close();
   		}
   }

   /**
    * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    */
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   }
}

你可能感兴趣的:(网页开发学习笔记——实现可刷新验证码)