index.html
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function reloadCode(){
var time = new Date().getTime();//根据时间的不同来刷新
document.getElementById("imagecode").src="<%=request.getContextPath() %>/servlet/ImageServlet?d="+time;
}
</script>
</head>
<body>
<form action="<%=request.getContextPath() %>/servlet/LoginServlet" method="get">
验证码:<input type="text" name="checkcode"/>
<img alt="验证码" id="imagecode" src="<%=request.getContextPath() %>/servlet/ImageServlet"/>
<a href="javascript: reloadCode();">看不清楚</a><br>
<input type="submit" value="提交">
</form>
</body>
</html>
ImageServlet.java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ImageServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{
BufferedImage bi=new BufferedImage(68, 22, BufferedImage.TYPE_INT_RGB);
Graphics g=bi.getGraphics();//获取画笔
Color c=new Color(200,150,255);//颜色
g.setColor(c);
g.fillRect(0, 0, 68, 22);//框
char[] ch="QWERTYUIOPASDFGHJKLZXCVBNM1234567890".toCharArray();
//获取四个字符
Random r=new Random();
int len=ch.length,index;//设置范围
StringBuffer sb=new StringBuffer();
for (int i = 0; i <4; i++) {
index=r.nextInt(len);
g.setColor(new Color(r.nextInt(88),r.nextInt(188),r.nextInt(255)));//有字符后放在颜色上
g.drawString(ch[index]+"",(i*15)+3,18);//字符转字符串 画字符
sb.append(ch[index]);
}//循环四次保存四次
request.getSession().setAttribute("piccode", sb.toString());///字符保存到session里,要进行验证
ImageIO.write(bi, "JPG",response.getOutputStream());
}
}
LoginServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException {
String piccod=(String)request.getSession().getAttribute("piccode");//获得到原来的验证码信息
String checkcode=request.getParameter("checkcode");//获取前台用户输入的数
response.setContentType("text/html;charset=gbk");//防止乱码
PrintWriter out=response.getWriter();//获得输出流
if(piccod.equals(checkcode)){
out.print("输入正确!");
}else{
out.print("输入错误!");
}
out.flush();//刷新
out.close();
}
}
web.xml
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>com.moke.ImageServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.moke.LoginServlet</servlet-class>
</servlet>
<!-- 映射关系 -->
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/servlet/ImageServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>
运行结果图:
ps:更新了web.xml记得从新启动服务器。