Servlet 实现验证码
2020-7-21 杨静
Servlet 实现验证码','分析
生成图片实现类
ImageServlet 类
① 定义BufferedImage 对象
② 获得 Graphics 对象
③ 通过 Random 产生随机验证码信息
④ 使用 Graphics 绘制图片
⑤ 记录验证码信息到 session 中
⑥ 使用 ImageIO 输出图片
代码实现
1、新建 web 项目 CheckCode,在 WebContent 下新建 index.jsp
index.jsp
- <%@ page language=\"java\" contentType=\"text/html; charset=UTF-8\"
- pageEncoding=\"UTF-8\"%>
- >
- <html>
- <head>
- <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
- <title>Insert title heretitle>
- <script type=\"text/javascript\">
- function reloadCode() {
- //传个时间参数,防止缓存
- var time = new Date().getTime();
- document.getElementById(\"imageCode\").src=\"<%=request.getContextPath()%>/ImageServlet?d=\"+time;
- }
- script>
- head>
- <body>
- <form action=\"<%=request.getContextPath()%>/LoginServlet\" method=\"post\">
- 验证码:<input type=\"text\" name=\"checkcode\" />
- <img id=\"imageCode\" src=\"<%=request.getContextPath()%>/ImageServlet\" alt=\"验证码\" />
- <a href=\"javascript:reloadCode();\">看不清楚a> <br />
- <input type=\"submit\" value=\"提交\"/>
- form>
- body>
- html>
2、在 src 下新建 com.liuyanzhao 包,然后分别新建用于动态画图的 ImageServlet 类和用于判断验证码是否正确的 LoginServlet 类
ImageServlet.java
- package com.liuyanzhao;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.image.BufferedImage;
- import java.io.IOException;
- 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;
- /*
- * @author LiuYanzhao
- */
- public class ImageServlet extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- BufferedImage bi = new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);
- Graphics g = bi.getGraphics();
- Color c = new Color(200,155,255);
- g.setColor(c);
- g.fillRect(0, 0, 68, 22);
- char[] ch = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\".toCharArray();
- Random r = new Random();
- int len = ch.length,index;
- StringBuffer sb = new StringBuffer();
- for(int i=0;i<4;i++) {
- index = r.nextInt(len);
- g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255)));
- g.drawString(ch[index]+\"\", (i*15)+3, 18);
- sb.append(ch[index]);
- }
- request.getSession().setAttribute(\"piccode\", sb.toString());
- ImageIO.write(bi, \"JPG\", response.getOutputStream());
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
- }
LoginServlet.java
- package com.liuyanzhao;
- 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;
- /*
- * @author LiuYanzhao
- */
- public class LoginServlet extends HttpServlet{
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- String piccode = (String)request.getSession().getAttribute(\"piccode\");
- String checkcode = request.getParameter(\"checkcode\");
- checkcode = checkcode.toUpperCase();//不区分大小写
- response.setContentType(\"text/html;charset=utf-8\");
- PrintWriter out = response.getWriter();
- if(piccode.equals(checkcode)) {
- out.println(\"验证码输入正确\");
- } else {
- out.println(\"验证码输入错误\");
- }
- out.flush();
- out.close();
- }
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doPost(request, response);
- }
- }
3、在 web.xml 里添加 Servlet 映射
- <servlet>
- <servlet-name>ImageServletservlet-name>
- <servlet-class>com.liuyanzhao.ImageServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>ImageServletservlet-name>
- <url-pattern>/ImageServleturl-pattern>
- servlet-mapping>
- <servlet>
- <servlet-name>LoginServletservlet-name>
- <servlet-class>com.liuyanzhao.LoginServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>LoginServletservlet-name>
- <url-pattern>/LoginServleturl-pattern>
- servlet-mapping>
4、运行 Tomcat 服务器,打开浏览器,访问 http://localhost:8080/CheckCode
效果如下
验证码不区分大小写,点击 “看不清楚”可以更换
发表评论: