(一)jsp页面
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>登录</title>
</head>
<body>
<form method="POST" action="loginchk">
<table>
<tr>
<td>name:</td>
<td><input type="text" name="username"/></td>
</tr>
<tr>
<td>password:</td>
<td><input type="password" name="password"/></td>
</tr>
<tr>
<td>checkNum:</td>
<td>
<input type="text" name="random" maxlength="4"/>
<img src="imgcode">
</td>
</tr>
<tr>
<td><input type="reset" value="reset"/></td>
<td><input type="submit" value="submit"/></td>
</tr>
</table>
</form>
</body>
</html>
(二)RandomCodeServlet和LoginCheckServlet
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;
import javax.servlet.http.HttpSession;
public class LoginCheckServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
HttpSession session = req.getSession();
String randomCode = (String)session.getAttribute("randomCode");
if(null==randomCode){
resp.sendRedirect("login.html");
return;
}
String reqRandom = req.getParameter("random");
req.setCharacterEncoding("GBK");
resp.setContentType("text/html;charset=GBK");
PrintWriter out = resp.getWriter();
if(randomCode.equalsIgnoreCase(reqRandom)){
out.print("验证码匹配!");
}else{
out.print("验证码校检失败,请重新输入!");
}
out.close();
}
}
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class RandomCodeServlet extends HttpServlet {
private int width = 60;
private int height = 20;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
//创建一个随机数字
Random random=new Random();
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
//创建字体,字体的大小应该根据图片的高度来决定
Font font = new Font("Times New Roman",Font.PLAIN,18);
//设置字体
g.setFont(font);
//画边框
g.setColor(Color.black);
g.drawRect(0, 0, width-1, height-1);
g.setColor(Color.gray);
//随机产生160条干扰线
for(int i=0; i<160;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
g.drawLine(x,y,x+x1,y+y1);
}
//RandomCode用于保存随机产生的验证玛,以便于用户登录后进行验证
StringBuffer randomCode = new StringBuffer();
int red = 0,green = 0,blue=0;
//随机产生4位数字的验证玛,
for(int i=0;i<4;i++){
//随机产生验证玛
String strRand = String.valueOf(random.nextInt(10));
//产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色都不一样
red = random.nextInt(110);
green = random.nextInt(50);
blue = random.nextInt(50);
//用随机产生的颜色将验证码绘制到图像中
g.setColor(new Color(red,green,blue));
g.drawString(strRand,13*i+6,16);
//将产生的四个随机数组合起来
randomCode.append(strRand);
}
//将四位数字保存到session中
HttpSession session = req.getSession();
session.setAttribute("randomCode", randomCode.toString());
//禁止图像的缓存
resp.setHeader("Pragma","no-cache");
resp.setHeader("Cache-Control","no-cache");
resp.setDateHeader("Expires",0);
resp.setContentType("image/jpeg");
//将图像输出到servlet输出流中
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg,"jpeg",sos);
sos.close();
}
}
(三)web.xml配置
<servlet>
<servlet-name>randomCodeServlet</servlet-name>
<servlet-class>RandomCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>randomCodeServlet</servlet-name>
<url-pattern>/imgcode</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>loginCheckServlet</servlet-name>
<servlet-class>LoginCheckServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>loginCheckServlet</servlet-name>
<url-pattern>/loginchk</url-pattern>
</servlet-mapping>