一、产生验证码的工具类
package blank.util;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class ImageUtil {
private static int width = 80;
private static int height = 30;
private static Random rd = new Random();
public static BufferedImage createImage(HttpServletRequest request) {
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
setBackgroud(g);
//绘制内容
String content=drawContent(g);
//存到session域中
request.getSession().setAttribute("content", content);
drawLines(g);
return image;
}
private static void drawLines(Graphics g) {
g.setColor(Color.orange);
//干扰线
for (int i = 0; i < 4; i++) {
int x1 = rd.nextInt(80);
int y1 = rd.nextInt(30);
int x2 = rd.nextInt(80);
int y2 = rd.nextInt(30);
g.drawLine(x1, y1, x2, y2);
}
}
private static String drawContent(Graphics g) {
g.setColor(Color.black);
String temp = "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740\u4e2a\u5730\u5230\u5927\u91cc\u8bf4\u5c31\u53bb\u5b50\u5f97\u4e5f\u548c\u90a3\u8981\u4e0b\u770b\u5929\u65f6\u8fc7\u51fa\u5c0f\u4e48\u8d77\u4f60\u90fd\u628a\u597d\u8fd8\u591a\u6ca1\u4e3a\u53c8\u53ef\u5bb6\u5b66\u53ea\u4ee5\u4e3b\u4f1a\u6837\u5e74\u60f3\u751f\u540c\u8001\u4e2d\u5341\u4ece\u81ea\u9762\u524d\u5934\u9053\u5b83\u540e\u7136\u8d70\u5f88\u50cf\u89c1\u4e24\u7528";
int len = temp.length();
StringBuffer sb = new StringBuffer();
for (int i = 0; i < 4; i++) {
int index = rd.nextInt(len);
sb.append(temp.charAt(index));
}
g.drawString(sb.toString(), 15, 18);
return sb.toString();
}
private static void setBackgroud(Graphics g) {
g.setColor(Color.gray);
g.fillRect(0, 0, width, height);
}
}
二、页面展示层
index.jsp
<body>
<c:if test="${user==null }">
<a href="./login.do?oper=pre">登录</a>
</c:if>
<c:if test="${user!=null }">
${user.name }
<a href="./login.do?oper=delete">注销</a>
</c:if>
</body>
login.jsp
<body>
<c:if test="${flag }"><!-- 防止非法访问 -->
<h3>用户登录</h3>
<form action="./login.do?oper=login" method="post">
<table>
${error }
<tr>
<td>用户名:</td>
<td><input type="text" name="name" value="${name}"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pass" value="${pass }"></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text"
name="checkCode"><img src="./code.do"
onclick="javascript:alert('刷新')" alt="验证码"/>${code }</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="登录"/></td>
</tr>
</table>
</form>
</c:if>
</body>
三、servlet控制层
private UserDao userDao = new UserDaoImpl();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String oper = request.getParameter("oper");
HttpSession session = request.getSession();
if ("pre".equals(oper)) {
session.setAttribute("flag", true);//防止非法访问
request.getRequestDispatcher("./login.jsp").forward(request,
response);
} else if ("login".equals(oper)) {
String content = (String) session.getAttribute("content"); //随机生成的验证码内容
if (content == null) {
request.getRequestDispatcher("./init.do").forward(request,
response);
}
String code = request.getParameter("checkCode");//用户输入的验证码
boolean flag = false;
if (code != null) {
if (code.equals(content)) { //验证码一致
flag = true;
}
}
if (flag) {
String name = request.getParameter("name");
String pass = request.getParameter("pass");
User user = null;
user = userDao.checkLogin(name, pass);
if (user != null) {
session.setAttribute("user", user);
request.getRequestDispatcher("./init.do").forward(request,
response);
} else {
request.setAttribute("error", "用户名或密码错误");
session.setAttribute("name", name);
session.setAttribute("pass", pass);
request.getRequestDispatcher("./login.jsp").forward(
request, response);
}
} else {
request.setAttribute("code", "验证错误");
request.getRequestDispatcher("./login.do?oper=pre").forward(
request, response);
}
} else if ("delete".equals(oper)) {
// session.invalidate();//删除session中所有的内存对象
session.removeAttribute("user");
request.getRequestDispatcher("./init.do")
.forward(request, response);
}
}
public class CheckCodeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
ImageIO.write(ImageUtil.createImage(request), "jpeg", response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}