2008-05-01
Servlet实现的图形验证码
Servlet实现的图形验证码
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
public class ValidateCode extends HttpServlet {
private Font mFont=new Font("宋体", Font.PLAIN,12);//设置字体
//处理post
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
doGet(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
//取得一个1000-9999的随机数
String s="";
int intCount=0;
intCount=(new Random()).nextInt(9999);//
if(intCount<1000)intCount+=1000;
s=intCount+"";
//保存入session,用于与用户的输入进行比较.
//注意比较完之后清除session.
HttpSession session=request.getSession (true);
session.setAttribute("validateCode",s);
response.setContentType("image/gif");
ServletOutputStream out=response.getOutputStream();
BufferedImage image=new BufferedImage(35,14,BufferedImage.TYPE_INT_RGB);
Graphics gra=image.getGraphics();
//设置背景色
gra.setColor(Color.yellow);
gra.fillRect(1,1,33,12);
//设置字体色
gra.setColor(Color.black);
gra.setFont(mFont);
//输出数字
char c;
for(int i=0;i<4;i++) {
c=s.charAt(i);
gra.drawString(c+"",i*7+4,11); //7为宽度,11为上下高度位置
}
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
}
java的图片处理包需要图形环境,而linux上没有启动图形环境,找不到图形环境的server(X11 window server using ':0.0' )所以会报这个错。而通过java -Djava.awt.headless=true 这个参数的指定就可以避免java 2d去找图形环境。
要么这样试试,应该也可以。在servlet里一开始写一句:
System.setProperty("java.awt.headless","true");
web服务器的java虚拟机必须加以个参数java.awt.headless=true
以tomcat为例
可以在/etc/profile或启动web服务的用户的.bash_profile中的CATALINA_OPTS变量中加入:
CATALINA_OPTS="... -Djava.awt.headless=true"
其他的也可以看看启动脚本。只要加上这个参数就没问题了。
import java.io.*;
import java.util.*;
import com.sun.image.codec.jpeg.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.awt.*;
import java.awt.image.*;
public class ValidateCode extends HttpServlet {
private Font mFont=new Font("宋体", Font.PLAIN,12);//设置字体
//处理post
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
doGet(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
//取得一个1000-9999的随机数
String s="";
int intCount=0;
intCount=(new Random()).nextInt(9999);//
if(intCount<1000)intCount+=1000;
s=intCount+"";
//保存入session,用于与用户的输入进行比较.
//注意比较完之后清除session.
HttpSession session=request.getSession (true);
session.setAttribute("validateCode",s);
response.setContentType("image/gif");
ServletOutputStream out=response.getOutputStream();
BufferedImage image=new BufferedImage(35,14,BufferedImage.TYPE_INT_RGB);
Graphics gra=image.getGraphics();
//设置背景色
gra.setColor(Color.yellow);
gra.fillRect(1,1,33,12);
//设置字体色
gra.setColor(Color.black);
gra.setFont(mFont);
//输出数字
char c;
for(int i=0;i<4;i++) {
c=s.charAt(i);
gra.drawString(c+"",i*7+4,11); //7为宽度,11为上下高度位置
}
JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(out);
encoder.encode(image);
out.close();
}
}
java的图片处理包需要图形环境,而linux上没有启动图形环境,找不到图形环境的server(X11 window server using ':0.0' )所以会报这个错。而通过java -Djava.awt.headless=true 这个参数的指定就可以避免java 2d去找图形环境。
要么这样试试,应该也可以。在servlet里一开始写一句:
System.setProperty("java.awt.headless","true");
web服务器的java虚拟机必须加以个参数java.awt.headless=true
以tomcat为例
可以在/etc/profile或启动web服务的用户的.bash_profile中的CATALINA_OPTS变量中加入:
CATALINA_OPTS="... -Djava.awt.headless=true"
其他的也可以看看启动脚本。只要加上这个参数就没问题了。
发表评论
- 浏览: 5626 次
- 性别:

- 来自: 广州

- 详细资料
搜索本博客
我的相册
me
共 1 张
共 1 张
最近加入圈子
最新评论
-
用java生成条形码,barcod ...
楼主:你说的那两个包下不下来啊,你是从哪个网站上下下来的?请发给我好吗?e-ma ...
-- by gloomboy -
条形码barcode4j的使用
看过了,有没有人在实际的项目中用过的?我需要在生成的条形码中加入中文的内容,不知 ...
-- by ffh205 -
用java生成条形码,barcod ...
输出中文字,我也需要这样的功能,能实现吗?
-- by 水雨田 -
用java生成条形码,barcod ...
看了,不错,能够生成条码,不过我需要在条码上输出有中文字,不知道楼主遇到过需要这 ...
-- by ffh205






评论排行榜