一、产生验证码的工具类
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 = "u7684u4e00u4e86u662fu6211u4e0du5728u4ebau4eecu6709u6765u4ed6u8fd9u4e0au7740u4e2au5730u5230u5927u91ccu8bf4u5c31u53bbu5b50u5f97u4e5fu548cu90a3u8981u4e0bu770bu5929u65f6u8fc7u51fau5c0fu4e48u8d77u4f60u90fdu628au597du8fd8u591au6ca1u4e3au53c8u53efu5bb6u5b66u53eau4ee5u4e3bu4f1au6837u5e74u60f3u751fu540cu8001u4e2du5341u4eceu81eau9762u524du5934u9053u5b83u540eu7136u8d70u5f88u50cfu89c1u4e24u7528";
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);
}
}