- 1.在body中加入如下代码:
- <script language="javascript">
- function changeCode() {
- document.getElementById('checkImg').src = 'GetCaptcha?temp='+ (new Date().getTime().toString(36));
- return false;
- }
- </script>
- <center>
- <form>
- 验证码: <input type="text" maxlength="113" οnmοuseοver="this.style.background='pink'" οnmοuseοut="this.style.background='white'">
- <img src="GetCaptcha" alt="验证码" name="checkImg" id="checkImg" onClick="changeCode()" />
- </form>
- </center>
- 2.在web.xml中配置如下代码:
- <servlet>
- <servlet-name>GetCaptcha</servlet-name>
- <servlet-class>com.eims.util.GetCaptcha</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>GetCaptcha</servlet-name>
- <url-pattern>/GetCaptcha</url-pattern>
- </servlet-mapping>
- 3.在com.eims.util.GetCaptcha包中加入如下工具类
- package com.eims.util;
- import java.io.IOException;
- 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 java.util.*;
- import java.awt.*;
- import java.awt.image.*;
- import javax.imageio.*;
- /**
- *
- * @author mzba
- *
- */
- public class GetCaptcha extends HttpServlet {
- private static final long serialVersionUID = 1L;
- /**
- * @see HttpServlet#HttpServlet()
- */
- public GetCaptcha() {
- super();
- // TODO Auto-generated constructor stub
- }
- /**
- * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
- * response)
- */
- protected void doGet(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- response.setContentType("image/jpeg");
- response.setHeader("Pragma", "No-cache");
- response.setHeader("Cache-Control", "no-cache");
- response.setDateHeader("Expires", 0);
- HttpSession session = request.getSession();
- int width = 75, height = 25;
- BufferedImage image = new BufferedImage(width, height,
- BufferedImage.TYPE_INT_RGB);
- Graphics g = image.getGraphics();
- Random random = new Random();
- g.setColor(getRandColor(200, 250));
- g.fillRect(0, 0, width, height);
- g.setFont(new Font("Times New Roman", Font.PLAIN, 24));
- g.setColor(getRandColor(160, 200));
- g.drawRect(0, 0, width - 1, height - 1);
- g.setColor(getRandColor(160, 200));
- for (int i = 0; i < 155; i++) {
- int x = random.nextInt(width);
- int y = random.nextInt(height);
- int xl = random.nextInt(12);
- int yl = random.nextInt(12);
- g.drawLine(x, y, x + xl, y + yl);
- }
- String sRand = "";
- for (int i = 0; i < 4; i++) {
- String rand = String.valueOf(random.nextInt(10));
- sRand += rand;
- g.setColor(new Color(20 + random.nextInt(110), 20 + random
- .nextInt(110), 20 + random.nextInt(110)));
- g.drawString(rand, 13 * i + 14, 20);
- }
- session.setAttribute("vcode", sRand);
- g.dispose();
- ImageIO.write(image, "JPEG", response.getOutputStream());
- }
- /**
- * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
- * response)
- */
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- // TODO Auto-generated method stub
- }
- Color getRandColor(int fc, int bc) {
- Random random = new Random();
- if (fc > 255)
- fc = 255;
- if (bc > 255)
- bc = 255;
- int r = fc + random.nextInt(bc - fc);
- int g = fc + random.nextInt(bc - fc);
- int b = fc + random.nextInt(bc - fc);
- return new Color(r, g, b);
- }
- }
- 4.在servlet中使用如下代码进行验证:
- /**
- * 验证用户输入的验证码输入的是否正确
- * @param request
- * @param response
- * @throws ServletException
- * @throws IOException
- */
- public boolean checkValidationCode(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- boolean flag=true;
- HttpSession session=request.getSession();
- String validationRightCode=(String)session.getAttribute("vcode");
- String validationUserCode=request.getParameter("validationCode");
- if(!validationRightCode.equals(validationUserCode)){
- request.setAttribute("information","验证码输入不正确!");
- flag=false;
- RequestDispatcher rd=request.getRequestDispatcher("login/dealwith.jsp");
- rd.forward(request, response);
- }
- return flag;
- }