首先是 login.html 表单如下
<form action="/demo2/login" method="post">
<input type="text" name="imagenum"><img alt="" src="/demo2/cap"><br>
<input type="submit" value="submit">
</form>
其次是画验证码的ImageServlet.java
import java.awt.Color;
import java.awt.Font;
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;
import javax.servlet.http.HttpSession;
public class ImageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取图片
// 创建图片
int width = 80;
int height = 40;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 创建图层 获得画板
Graphics g = image.getGraphics();
// 确定使用的画笔颜色
g.setColor(Color.BLACK);
// 填充矩形
g.fillRect(0, 0, width - 2, height - 2);
String dataString = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// 设置字体
g.setFont(new Font("宋体", Font.BOLD, 30));
// 缓存随机生成的字符
StringBuffer buf = new StringBuffer();
Random random = new Random();
for (int i = 0; i < 4; i++) {
// 设置随机颜色
g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
// 获得一个随机字符
int index = random.nextInt(62);
String str = dataString.substring(index, index + 1);
g.drawString(str, 20 * i, 30);
// 缓存
buf.append(str);
}
// 干扰线
for (int i = 0; i < 5; i++) {
g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
g.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height));
}
HttpSession session = request.getSession();
session.setAttribute("cap", buf.toString());
response.setContentType("image/jpeg");
// 将图片发送给浏览器
ImageIO.write(image, "jpg", response.getOutputStream());//图片 格式 输出流
}
}
然后是验证 LoginServlet.java
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 LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String imagenum = request.getParameter("imagenum");
PrintWriter writer = response.getWriter();
if (imagenum != null && imagenum != "") {
HttpSession session = request.getSession();
String cap = session.getAttribute("cap").toString();
if (cap.equalsIgnoreCase(imagenum)) {
writer.write("验证通过");
} else {
writer.write("验证失败");
Thread thread = new Thread();
try {
thread.sleep(1000);
response.sendRedirect("/demo2/login.html");
} catch (Exception e) {
}
}
// 无论什么情况 只能使用一次
session.removeAttribute("cap");
} else {
writer.write("请填写参数");
}
}
}
网友评论