1.设计web 页面
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <style type="text/css"> .s1{ cursor:pointer; } </style> <title>验证码</title> </head> <body> <form action="code.action" method="post"> 验证码:<input type="text" name="vcode"> <img src="validate.code" onclick="this.src=‘ValidateCode.code?‘+Math.random()" class="s1" title="点击刷新"> <% Object err=(request.getAttribute("code_err")); %> <span style="color:red"><%=err==null?"":err %></span> <input type="submit" value="确定"> </form> </body> </html>
2. 使用java代码画出验证码的图片
package day4; 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.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.catalina.connector.OutputBuffer; import org.apache.catalina.tribes.membership.StaticMember; /** * Servlet implementation class ValidateCode */ public class ValidateCode extends HttpServlet { Random r=new Random(); protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session =request.getSession(); BufferedImage image=new BufferedImage(100, 20, BufferedImage.TYPE_INT_RGB); Graphics g=image.getGraphics(); g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255))); g.fillRect(0, 0, 100, 20); String content=getContent(3); g.setColor(new Color(0, 0, 0)); g.setFont(new Font(null, Font.BOLD, 18)); g.drawString(content, 30, 15); for (int i = 0; i < 5; i++) { g.drawLine(100, 20, r.nextInt(100), r.nextInt(20)); } session.setAttribute("code", content); response.setContentType("text/html"); OutputStream os=response.getOutputStream(); ImageIO.write(image, "jpeg", os); os.close(); } public String getContent(int size) { String str="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; String content=""; for (int i = 0; i < size; i++) { content+=str.charAt(r.nextInt(str.length())); } return content; } }
3. 服务端检验验证码是否相同
package day2; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class userAction */ public class userAction extends HttpServlet { protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter pw=response.getWriter(); String code=(String) session.getAttribute("code"); String vcode=request.getParameter("vcode"); if(code.equals(vcode)) { pw.print(code+"<br><p>相等</p>"); }else { request.setAttribute("code_err", "验证码错误"); request.getRequestDispatcher("validate.jsp").forward(request, response); } } }
end