首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

jsp页面如何调用servlet中生成的验证码

2012-01-18 
jsp页面怎么调用servlet中生成的验证码生成验证码的java类:Java code //用于获取四位随机数private char m

jsp页面怎么调用servlet中生成的验证码
生成验证码的java类:

Java code
 //用于获取四位随机数     private char mapTable[] = {'0','1','2','3','4','5','6','7','8','9'};     //生成验证码,并返回随机生成的数字     public String getEnsure(int width, int height, OutputStream os){         if (width <= 0)             width = 60;         if (height <= 0)             height = 20;         BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);         // 获取图形上下文         Graphics g = image.getGraphics();         // 设定背景色         g.setColor(new Color(0xDCCCCC));         g.fillRect(0, 0, width, height);         // 画边框         g.setColor(Color.black);         g.drawRect(0, 0, width - 1, height - 1);         // 取随机产生的认证码         String strEnsure = "";         // 4代表4位验证码         for (int i = 0; i < 4; ++i){             strEnsure += mapTable[(int) (mapTable.length * Math.random())];         }         // 将认证码显示到图象中         g.setColor(Color.red);         g.setFont(new Font("Atlantic Inline", Font.PLAIN, 14));         // 画的具体坐标         String str = strEnsure.substring(0, 1);         g.drawString(str, 8, 14);         str = strEnsure.substring(1, 2);         g.drawString(str, 20, 15);         str = strEnsure.substring(2, 3);         g.drawString(str, 35, 18);         str = strEnsure.substring(3, 4);         g.drawString(str, 45, 15);         // 释放图形上下文         g.dispose();         try{             // 输出图象到页面             ImageIO.write(image, "JPEG", os);         } catch (IOException e){             return "";         }                  return strEnsure;     }


下面是servlet
Java code
public class CheckServlet extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        doPost(request, response);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //禁用缓存,每次访问此页面,都重新生成        response.setHeader("Pragma","No-cache");         response.setHeader("Cache-Control","no-cache");         response.setDateHeader("Expires", 0);         //生成验证码的实例对象        CheckCode ie = new CheckCode();        //调用里面的方法,返回的是生成的验证码中的字符串        String str = ie.getEnsure(0,0,response.getOutputStream());        //获得session,并把字符串保存在session中,为后面的对比做基础        HttpSession session = request.getSession();        session.setAttribute("strEnsure", str);                  }}


用了多种方法尝试就是没有成功
img 中的src的属性应该怎么指向servlet???



[解决办法]
折子戏方法没试过!我一直用的是将验证码写在一个jsp页面中
这样调用:
<img src="code.jsp" id="imgId">
  
code.jsp如下:
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%!




char codeTable[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9' };


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);
}
%>
<%
//设置页面不缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);

// 在内存中创建图象
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

// 获取图形上下文
Graphics g = image.getGraphics();

//生成随机类
Random random = new Random();

// 设定背景色
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);

//设定字体
g.setFont(new Font("Times New Roman",Font.PLAIN,18));

//画边框
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);

// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
 int x = random.nextInt(width);
 int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
 g.drawLine(x,y,x+xl,y+yl);
}

// 取随机产生的认证码(4位数字)
String sRand="";
for (int i=0;i<4;i++){
// String rand=String.valueOf(random.nextInt(10));
String rand=String.valueOf(codeTable[(int) (codeTable.length * Math.random())]);

sRand+=rand;
// 将认证码显示到图象中
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
}

// 将认证码存入session
session.setAttribute("rand",sRand);
// 图象生效
g.dispose();

// 输出图象到页面
try{
ImageIO.write(image, "JPEG", response.getOutputStream());
out.clear();
out = pageContext.pushBody();
}catch(Exception e){
e.getMessage();
}
%>
楼主的这种方法 期待高人指点 呵呵学习了
[解决办法]
这种路径问题可以在浏览器地址栏输入全路径测试
假如servlet是下面这样配置的

XML code
<servlet>     <display-name>CheckServlet</display-name>      <servlet-name>CheckServlet</servlet-name>     <servlet-class>com.wangxf.servlet.CheckServlet</servlet-class>    </servlet>    <servlet-mapping>    <servlet-name>CheckServlet</servlet-name>    <url-pattern>/servlet/CheckServlet</url-pattern>    </servlet-mapping> 

热点排行