使用Servlet实现验证码
原创
©著作权归作者所有:来自51CTO博客作者流年若逝的原创作品,请联系作者获取转载授权,否则将追究法律责任
使用Servlet实现验证码制作主要思路
<img alt="验证码" src="<%=request.getContextPath()%>/servlet/ImageServlet" />
运用Servlet随机生成图片,用到的类:
1. BufferedImage图像数据缓冲区
2. Graphics绘制图片
3. Color获取颜色
4. Random生成随机数
5. ImageIO输出图片
ImageServlet类
1. 定义BufferedImage对象
2. 获取Graphics对象
3. Random随机生成验证码信息
4. 使用Graphics绘制图片
5. 验证码信息放入StringBuffer缓冲区,再传入Session中
6. 使用ImageIO输出图片
校验验证码是否正确
CheckServlet类
1. 获取页面提交的验证码
2. 获取Session中的验证码
3. 信息校验
4. 返回结果
(ps:我用的是Eclipse与Myeclipse有点不同 但是代码都是一样的,一下是我的代码块图片展示)
实验要达到的效果如下:
接下来是代码展示
1.ImageServlet.java
package com.check;
import java.awt.Color;
import java.awt.Graphics;
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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".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());
//输出图片
ImageIO.write(bi, "JPG", response.getOutputStream());
}
}
2.LoginServlet.java
package com.check;
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 piccode = (String) request.getSession().getAttribute("piccode");
String checkcode = request.getParameter("checkcode");
//输入验证码不需要区分大小写
checkcode = checkcode.toUpperCase();
//纠正显示在网页上的乱码
response.setContentType("text/html;charset=gbk");
PrintWriter out = response.getWriter();
if(checkcode.equals(piccode)){
out.println("验证码输入正确!");
}else{
out.println("验证码输入错误!!!");
}
out.flush();
out.close();
}
}
3.web.xml
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>com.check.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/servlet/ImageServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.check.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet/LoginServlet</url-pattern>
</servlet-mapping>
4.index.jsp
<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="post">
验证码:<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>
以上就是servlet制作验证码的基本流程,有兴趣的小伙伴们可以在我的网盘下载更多的其他验证方式,比如:算术验证,中文验证等
链接:https://pan.baidu.com/s/1hrFpqTm 密码:a1g6