login.jsp:
login
window.onload = function () {
document.getElementById("img").onclick = function () {
this.src = "/checkCodeServlet1?time="+new Date().getTime();
}
}
div{
color: red;
}
用户名
密码
验证码
CheckCodeServlet:
@WebServlet("/checkCodeServlet1")
public class CheckCodeServlet1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width = 100;
int height = 50;
//1、创建一对象,在内存中画图(验证码图片对象)
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//2、美化图片
//填充背景色
Graphics g = image.getGraphics();
g.setColor(Color.PINK);
g.fillRect(0,0,width,height);
//画边框
g.setColor(Color.BLUE);
g.drawRect(0,0,width-1,height-1);
//写验证码
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789";
//生成随机角标
Random ra = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 1; i |