当前位置: 代码迷 >> J2EE >> jsp页面如何调用servlet中生成的验证码
  详细解决方案

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

热度:223   发布时间:2016-04-22 02:37:02.0
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);
  相关解决方案