Form.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP ‘Confirm.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <script type="text/javascript"> function changeImage(img) { //document.getElementsByName("img"); //alert("aaaa"); img.src = img.src + "?"+ new Date().getTime(); } </script> </head> <body> <form name="form" action="/ConfirmDynamicPictureDemo/servlet/ImageCheckServlet" method="post"> 验证码: <input type="text" name="info"><image name ="img" src="/ConfirmDynamicPictureDemo/servlet/ImageProduceServlet" alt="图片" onclick="changeImage(this)"/> <br/> <input type="submit" value="提交" > </form> </body> </html> |
ImageProduceServlet.java
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ImageProduceServlet extends HttpServlet { private static final int WIDTH = 120; private static final int HIGHT = 35; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BufferedImage image = new BufferedImage(WIDTH, HIGHT, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); //设置背景颜色 setBackGroud(g); //设置边框 setBorder(g); //画干扰线 drawRandomLine(g); //写随机数,并且存入session String random = drawRandomNum((Graphics2D)g); request.getSession().setAttribute("imageCode", random); //图形写给浏览器 response.setContentType("image/jpeg"); ImageIO.write(image, "jpg", response.getOutputStream()); } private void setBackGroud(Graphics g) { g.setColor(Color.GRAY); g.fillRect(0, 0, WIDTH, HIGHT); } private void setBorder(Graphics g) { g.setColor(Color.BLUE); g.drawRect(1,1,WIDTH-2,HIGHT-2); } private void drawRandomLine(Graphics g) { g.setColor(Color.GREEN); for(int i=0;i<5;i++) { int x1 = new Random().nextInt(WIDTH); int y1 = new Random().nextInt(HIGHT); int x2 = new Random().nextInt(WIDTH); int y2 = new Random().nextInt(HIGHT); g.drawLine(x1,y1,x2,y2); } } private String drawRandomNum(Graphics2D g) { g.setColor(Color.RED); g.setFont(new Font("宋体",Font.BOLD,20)); String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; StringBuffer sb = new StringBuffer(); int x =5; for(int i=0; i<4;i++) { int degree = new Random().nextInt()%30; //-30度~30度之间 String ch = base.charAt(new Random().nextInt(base.length()))+""; sb.append(ch); g.rotate(degree*Math.PI/180,x,20); g.drawString(ch, x, 20); g.rotate(-degree*Math.PI/180,x,20); //保证旋转不是累加的 x+=30; } return sb.toString(); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } |
ImageCheckServlet.java
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ImageCheckServlet 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 { response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); //校验认证码是否有效 String client_code = request.getParameter("info"); String server_code = (String) request.getSession().getAttribute("imageCode"); //没有忽略大小写 if(client_code==null || server_code==null || !server_code.equals(client_code)) { System.out.println("验证码不正确..."); return; } System.out.println("处理客户机请求..."); } } |
JavaEE动态图片验证
时间: 2024-10-29 19:09:40