package image;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ImageCode extends HttpServlet {
/**
* Constructor of the object.
*/
public ImageCode() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try{
int width = 50;
int height = 20;
response.setContentType("image/jpeg");
String s = "";
Random random = new Random();
for(int i=0; i<4; i++) {
s += String.valueOf(random.nextInt(9));
}
System.out.println("验证码:"+s);
HttpSession session = request.getSession();
session.setAttribute("validataCode", s);
ServletOutputStream out = response.getOutputStream();
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphics g = image.getGraphics();
g.setColor(getRandColor(200, 250));
//填充矩形
g.fillRect(0, 0, width, height);
Font mFont = new Font("Times New Roman", Font.BOLD, 18);// 设置字体
g.setFont(mFont);
g.setColor(getRandColor(160, 200));
Random r = new Random();
//随机的生成一些线
for (int i = 0; i < 155; i++) {
int x2 = r.nextInt(width);
int y2 = r.nextInt(height);
int x3 = r.nextInt(12);
int y3 = r.nextInt(12);
g.drawLine(x2, y2, x2 + x3, y2 + y3);
}
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
g.drawString(s, 2, 16);
g.dispose();
// 输出图象到页面
ImageIO.write((BufferedImage) image, "JPEG", out);
out.close();
}catch(Exception e) {
e.printStackTrace();
}
}
private Color getRandColor(int fc, int bc) { // 给定范围获得随机颜色
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
WEB.XML配置
<servlet>
<servlet-name>ImageCode</servlet-name>
<servlet-class>image.ImageCode</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageCode</servlet-name>
<url-pattern>/servlet/ImageCode</url-pattern>
</servlet-mapping>
页面显示,需要jquery
<script type="text/javascript" src="js/jquery-1.4.js"></script>
<img id="vcode_img" src="<%=request.getContextPath() %>/servlet/ImageCode">
<a href="#" onClick="$('#vcode_img').attr('src','<%=request.getContextPath() %>/servlet/ImageCode?t='+(new Date().getTime()));return false;">看不清楚,换一张</a>
分享到:
相关推荐
java web笔记之servlet生成验证码java web笔记之servlet生成验证码
servlet生成验证码图片
使用Servlet生成验证码图片。 一个生成验证码图片的工具类。
正在学习Java,在教程书和网上不断的学习。servlet 提供了 Java 强大的 web 交互,在这个小项目中,我用servlet 实现了在web上生成验证码的功能,正在入门Java的同学不妨试试。
通过servlet生成验证码的小工具类,使用时在web.xml里面配置,页面上引用时利用img标签引用这个servlet就可以实现验证码实例: <img src="checkCode.do" id="img" height="30px"></a> 实现点击图片时更换验证码
通过Servlet生成图片验证码,提供给jsp页面展示,并验证。
韩顺平Servlet中用到的生成验证码源码
这是一个完整的功能,实现了动态生成验证码及验证填入信息是否正确的功能,是入门者非常不错的参考资料,是我在课堂上演示的代码。
jsp+servlet动态生成验证码,点击换一份张时改变,供初学者借鉴
登陆验证+servlet动态生成图片验证码
主要介绍了Javaweb开发中通过Servlet生成验证码图片的相关资料,需要的朋友可以参考下
Servlet随即产生验证码,在页面显示。通过Servlet验证。简单实用。
NULL 博文链接:https://yangweixu.iteye.com/blog/1973432
Servlet动态生成验证码,运行Tomcat在浏览器输入地址即可。