protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{ BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR); Graphics2D g = buffImg.createGraphics(); //产生Random Random random = new Random();
g.setColor(Color.WHITE); g.fillRect(0,0,width,height); //字体 Font font = new Font("Dialog",Font.PLAIN,18); g.setFont(font); //边框 g.setColor(Color.BLACK); g.drawRect(0,0,width-1,height-1);
//产生160条干扰线,图像中的认证码,不被看到 g.setColor(Color.GRAY); for(int i=0;i<160;i++){ int x = random.nextInt(width); int y = random.nextInt(height); int x1 = random.nextInt(12); int y1 = random.nextInt(12); g.drawLine(x,y,x1,y1);
}
StringBuffer randomCode = new StringBuffer();//保存验证码 int red = 0,green = 0,blue = 0;
for(int i = 0;i < 4;i++){ String strRand=String.valueOf(random.nextInt(10)); //Color red = random.nextInt(110); green = random.nextInt(50); blue = random.nextInt(50); //set Color g.setColor(new Color(red,green,blue)); g.drawString(strRand,13*i+6,16); randomCode.append(strRand);//保存验证码 } //搞验证码 到SESSION中去 HttpSession session=req.getSession(); session.setAttribute("randdomCode",randomCode.toString());
那我说下我自己知道的两种方式吧
1.由于每秒的图象都不一样,所以每次更新图象之前我们都将原来的图象更新成白屏,然后画上新的图象
这种方法比较类似简单而且比较容易实现,但是也有他的缺点:由于每次都需要将整个屏幕都刷新一次,所以很容易发生屏闪的现象,从我自己以前实现的情况,这种屏闪还是很严重的,可能是我个人技术不到位吧
2.由于第1种方法的缺陷,我们需要将刷新的部位尽可能的缩小,只刷新改动的部分,repaint方法拉.
这个方法是比较好,但是同时因为要事先知道每次刷新的改动在图形绘制的过程中是不大切实的,也就比较局限了.
LZ要求要有具体的代码,我可能需要帮你去找找,最近找工作呢,很久都没有动代码了.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.*;
import java.awt.image.BufferedImage;import java.util.Random;
import javax.imageio.ImageIO;public class RandomCodeServlet extends HttpServlet{
private int width = 60;//验证码的宽度
private int height = 20;//验证码的高度
protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
BufferedImage buffImg = new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
Graphics2D g = buffImg.createGraphics();
//产生Random
Random random = new Random();
g.setColor(Color.WHITE);
g.fillRect(0,0,width,height);
//字体
Font font = new Font("Dialog",Font.PLAIN,18);
g.setFont(font);
//边框
g.setColor(Color.BLACK);
g.drawRect(0,0,width-1,height-1);
//产生160条干扰线,图像中的认证码,不被看到
g.setColor(Color.GRAY);
for(int i=0;i<160;i++){
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(12);
int y1 = random.nextInt(12);
g.drawLine(x,y,x1,y1);
}
StringBuffer randomCode = new StringBuffer();//保存验证码
int red = 0,green = 0,blue = 0;
for(int i = 0;i < 4;i++){
String strRand=String.valueOf(random.nextInt(10));
//Color
red = random.nextInt(110);
green = random.nextInt(50);
blue = random.nextInt(50);
//set Color
g.setColor(new Color(red,green,blue));
g.drawString(strRand,13*i+6,16);
randomCode.append(strRand);//保存验证码
}
//搞验证码 到SESSION中去
HttpSession session=req.getSession();
session.setAttribute("randdomCode",randomCode.toString());
//禁止图像缓存
resp.setHeader("Pragma","no-cache");
resp.setHeader("Cache-Control","no-cache");
resp.setDateHeader("Expires",0);
resp.setContentType("image/jpeg");
//将图像输出到Servlet输出 流中
ServletOutputStream sos=resp.getOutputStream();
ImageIO.write(buffImg,"jpeg",sos);
sos.close();
}
}
/////////////////////package com.rand;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginCheckServlet extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
HttpSession session = req.getSession();
String randomCode = (String)session.getAttribute("randomCode");
if (randomCode == null){
resp.sendRedirect("login.html");
return;
}
String reqRandom = req.getParameter("random");
req.setCharacterEncoding("GBK");
resp.setContentType("text/html;charset=GBK");
PrintWriter out = resp.getWriter();
if(randomCode.equals(reqRandom)){
out.println("验证码一致");
}else{
out.println("验证码不一致");
}
out.close();
}
}/////////////////<html>
<head>
<title>登录页面</title>
</head>
<body>
<form method="post" action="loginchk">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr> <tr>
<td>密 码:</td>
<td><input type="password" name="password" size=21></td>
</tr> <tr>
<td>验证码:</td>
<td><input type="text" name="random" maxlength=4><img src="imgcode"></td>
</tr> <tr>
<td><input type="submit" value="登 录"></td>
<td><input type="reset" value="重 填"></td>
</tr>
</table>
</form>
</body>
</html>/////////////<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5"> <servlet>
<servlet-name>RandomCodeServlet</servlet-name>
<servlet-class>com.rand.RandomCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RandomCodeServlet</servlet-name>
<url-pattern>/imgcode</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LoginCheckServlet</servlet-name>
<servlet-class>com.rand.LoginCheckServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginCheckServlet</servlet-name>
<url-pattern>/loginchk</url-pattern>
</servlet-mapping>s</web-app>