龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > Jsp编程 >

JSP验证码实现随机背景色及字体颜色,带扰乱线条效果源代码(2)

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
Code [http://www.xueit.com] 1 % @ page language = " java " contentType = " image/jpeg ; charset=UTF-8 " 2 pageEncoding = " UTF-8 " import = " java.awt.*,java.awt.image.*,javax.imageio.*,java.io.* " %

Code [http://www.xueit.com]
1 <%@ page language="java" contentType="image/jpeg ; charset=UTF-8" 2 pageEncoding="UTF-8" import="java.awt.*,java.awt.image.*,javax.imageio.*,java.io.*"%> 3 <%@ page import ="com.CreatImage"%> 4 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 5 <html> 6 <head> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <title>Insert title here</title> 9 </head> 10 <body> 11 <% 12 response.setHeader("Pragma","No-cache"); 13 response.setHeader("Cache-Control","no-cache"); 14 response.setDateHeader("Expires", 0); 15 16 CreatImage c = new CreatImage(request,response); 17 BufferedImage image = c.creatImage(); 18 OutputStream b = response.getOutputStream(); 19 %> 20 <% 21 ImageIO.write(image, "JPEG", response.getOutputStream()); 22 //必须添加以下两行否则tomcat下jsp出现getOutputStream() has already been called for this response异常 23 out.clear(); 24 out = pageContext.pushBody(); 25 %> 26 </body> 27 </html>

注意   contentType="image/jpeg ; charset=UTF-8"

Code [http://www.xueit.com]
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <% 4 String path = request.getContextPath(); 5 %> 6 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 7 <html> 8 <head> 9 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 10 <title>Insert title here</title> 11 <script> 12 function changeCode() 13 { 14 document.getElementById("cc").src="<%=path%>/images.jsp?a=" Math.random(); 15 } 16 </script> 17 </head> 18 <body> 19 <form action="" method="post"> 20 <table cellspacing="2px" cellpadding="2px" align="center"> 21 <tr> 22 <td>用户名:</td> 23 <td><input type="text" id="name" value=""/></td> 24 </tr> 25 <tr> 26 <td>&nbsp;&nbsp;码:</td> 27 <td><input type="password" id="password" value=""/></td> 28 </tr> 29 <tr height="26px"> 30 <td>验证码:</td><td valign="middle"><input type="text" id="validate"></td> 31 <td><a href="#" onclick="changeCode();"><img alt="" border="0" id="cc" src="<%=path %>/images.jsp"/></a></td> 32 </tr> 33 <tr> 34 <td colspan="2" align="center"><input type="button" name="button" value="submit"/></td> 35 </tr> 36 </table> 37 </form> 38 </body> 39 </html>

精彩图集

赞助商链接