1. jsp代码 :
1 <Script> 2 function changeImg(){ 3 document.getElementById("certImg").src ="makeCertPic.jsp?it="+Math.random(); /* +Math.random() */ 4 } 5 </Script> 6 7 8 9 <table width="100%" border="0" cellspacing="1" cellpadding="1"> 10 <tr> 11 <td width="5%"> </td> 12 <td nowrap width="11%" align="left" class="loginname">验证码: 13 14 </td> 15 <td nowrap width="68%" class="loginname"> 16 <div align="left"> 17 <input type="text" istyle="input01" name="certCode" 18 class="input01" /> 19 <!-- <iframe width="200" height="60" id="certImg" src="makeCertPic.jsp"></iframe> --> 20 <img id="certImg" src="makeCertPic.jsp" onclick="changeImg()" alt="看不清换一张"> 21 <!-- onclick="changeImg()" alt="看不清换一张" --> 22 23 </div> 24 25 </td> 26 27 </tr> 28 29 <tr> 30 <td width="14%"> </td> 31 32 <td nowrap width="18%" align="right" class="loginname"> 33 <div align="left">用户名:</div> 34 </td> 35 <td nowrap width="68%" class="loginname"> 36 <div align="left"> 37 <html:text styleClass="input01" property="user_code" 38 onkeypress="return handleEnter(this, event,1)" /> 39 <!-- <a href="#" onclick="changeImg()">看不清换一张 </a> --> 40 </div> 41 </td> 42 </tr> 43 <tr> 44 <td width="14%"> </td> 45 <td nowrap width="18%" align="right" class="loginname"> 46 <div align="left">密 码:</div> 47 </td> 48 <td nowrap width="68%" class="loginname"> 49 <div align="left"> 50 <html:password styleClass="input01" property="password" 51 value="" /> 52 </div> 53 </td> 54 55 </tr> 56 57 <tr> 58 59 60 <td align="left"> 61 <td nowrap align="right"><html:submit value='登 录' 62 styleClass="button" onclick="return ck_login();" /></td> 63 <td nowrap align="left"><html:reset value='重 写' 64 styleClass="button" /></td> 65 </td> 66 </tr> 67 </table>
2.链接的加jsp文件,用于加载验证码数据
1 <%@page import="com.test.makeCertPic"%> 2 <%@ page language="java" import="java.util.*" pageEncoding="GBK"%> 3 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 4 <%@page contentType="image/jpeg" %> 5 6 <% 7 makeCertPic pic = new makeCertPic(); 8 String str = pic.getCertPic(0, 0, response.getOutputStream()); 9 session.setAttribute("certCode", str); 10 //关闭response ,不加会运行两次,且会报错 11 out.clear(); 12 out = pageContext.pushBody(); 13 %>
3.对应的验证码类 java代码
1 package com.test; 2 3 import java.awt.Color; 4 import java.awt.Font; 5 import java.awt.Graphics; 6 import java.awt.image.BufferedImage; 7 import java.io.IOException; 8 import java.io.OutputStream; 9 import java.util.Random; 10 import javax.imageio.ImageIO; 11 12 /** 13 * @author dzy 生成验证码图片 14 */ 15 public class makeCertPic { 16 // 验证码图片中可以出现的字符集,可根据需要修改 17 private char mapTable[] = { '0','1', '2', '3', '4', '5', '6', '7', '8','9' }; 18 19 /** 20 * 功能:生成彩色验证码图片 参数width为生成图片的宽度,参数height为生成图片的高度,参数os为页面的输出流 21 */ 22 public String getCertPic(int width, int height, OutputStream os) { 23 if (width <= 0) 24 width = 60; 25 if (height <= 0) 26 height = 20; 27 BufferedImage image = new BufferedImage(width, height, 28 BufferedImage.TYPE_INT_RGB); 29 // 获取图形上下文 30 Graphics g = image.getGraphics(); 31 // 设定背景色 32 g.setColor(new Color(0xDCDCDC)); 33 g.fillRect(0, 0, width, height); 34 // 画边框 35 g.setColor(Color.black); 36 g.drawRect(0, 0, width - 1, height - 1); 37 // 取随机产生的认证码 38 String strEnsure = ""; 39 // 4代表4位验证码,如果要生成更多位的认证码,则加大数值 40 for (int i = 0; i < 4; ++i) { 41 strEnsure += mapTable[(int) (mapTable.length * Math.random())]; 42 } 43 // 将认证码显示到图像中,如果要生成更多位的认证码,增加drawString语句 44 g.setColor(Color.black); 45 g.setFont(new Font("Atlantic Inline", Font.PLAIN, 18)); 46 String str = strEnsure.substring(0, 1); 47 g.drawString(str, 8, 17); 48 str = strEnsure.substring(1, 2); 49 g.drawString(str, 20, 15); 50 str = strEnsure.substring(2, 3); 51 g.drawString(str, 35, 18); 52 str = strEnsure.substring(3, 4); 53 g.drawString(str, 45, 15); 54 // 随机产生10个干扰点 55 Random rand = new Random(); 56 for (int i = 0; i < 10; i++) { 57 int x = rand.nextInt(width); 58 int y = rand.nextInt(height); 59 g.drawOval(x, y, 1, 1); 60 } 61 // 释放图形上下文 62 g.dispose(); 63 try { 64 // 输出图像到页面 65 ImageIO.write(image, "JPEG", os); 66 } catch (IOException e) { 67 return ""; 68 } 69 return strEnsure; 70 } 71 }
项目中的 验证码 尤其注意登陆时会被项目的filter类过滤排除,导致验证码加载不出来 ,需要在web.xml中 进行jsp的自定义过滤
本项目中设置filter的xml文件 和 过滤的java代码如下:
1 <filter> 2 <filter-name>BtSystemFilter</filter-name> 3 <filter-class>com.bettersoft.filters.BtSystemFilter</filter-class> 4 </filter> 5 <filter-mapping> 6 <filter-name>BtSystemFilter</filter-name> 7 <url-pattern>/*</url-pattern> 8 </filter-mapping>
1 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException { 2 HttpServletRequest request = (HttpServletRequest) req; 3 HttpServletResponse response = (HttpServletResponse) resp; 4 HttpSession session = request.getSession(); 5 String uri = request.getRequestURI(); 6 String projectName = request.getContextPath(); 7 String url=uri.substring(projectName.length()); 8 try 9 { 10 if( uri.startsWith(projectName + "/services") 11 || uri.startsWith(projectName + "/axis2-web")) //webservice 12 { 13 chain.doFilter(req, resp); 14 } 15 //过滤.jsp和.do 16 else if (uri.endsWith(".jsp")|| uri.endsWith(".do")) 17 { 18 //如果为系统登录类型资源不进行session检查 19 if((uri.equals(projectName+"/index.jsp")) 20 ||uri.equals(projectName+"/login.jsp") 21 ||uri.equals(projectName+"/main.jsp") 22 ||uri.equals(projectName+"/login.do") 23 ||uri.equals(projectName+"/showTRCA.jsp")//用证书登录时选择证书 24 ||uri.equals(projectName+"/bindCA.jsp")//用证书登录时绑定证书 25 ||uri.equals(projectName+"/error.jsp") 26 ||uri.equals(projectName+"/makeCertPic.jsp")//验证码 27 ) 28 { 29 chain.doFilter(req, resp); 30 } 31 else 32 { //session失效或未登录 33 if (session.getAttribute("com.bettersoft.admin.userbean") == null) 34 { response.sendRedirect(projectName + "/login.jsp?nologin=nologin"); 35 } 36 else 37 { //如果当前资源是系统菜单 38 if(CheckUserUtil.isSyeRes(url)) 39 { 40 if(CheckUserUtil.hasPermission(request, url)) 41 { 42 chain.doFilter(req, resp); 43 } 44 else 45 { 46 response.sendRedirect(projectName + "/login.jsp?nologin=invalidurl"); 47 } 48 } 49 else 50 { 51 chain.doFilter(req, resp); 52 } 53 54 } 55 } 56 }else 57 { 58 chain.doFilter(req, resp); 59 } 60 } 61 catch(Exception e) 62 { e.printStackTrace(); 63 String errormsg="未知的系统异常"; 64 if(e.getMessage()!=null) 65 { 66 //if(e.getMessage().length()<50) 67 errormsg=e.getMessage(); 68 } 69 request.setAttribute("errormsg",errormsg); 70 request.getRequestDispatcher("/error.jsp").forward(request, response); 71 } 72 }
最后的效果图