javascript
Spring MVC 验证码
頁面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
? <head>
? ? <base href="<%=basePath%>">
? ??
? ? <title>My JSP 'code.jsp' starting page</title>
? ??
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0"> ? ?
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script src="js/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
?function changeImg() {
? var imgSrc = $("#imgObj");
? var src = imgSrc.attr("src");
? //alert(src);
? imgSrc.attr("src", chgUrl(src));
? alert($("#code").val());
? //$("#code").val("123456");?
? //<c:out value="${sessionScope.code}"/>
?}
?//時間戳 ?
?//為了使每次生成圖片不一致,即不讓瀏覽器讀緩存,所以需要加上時間戳 ?
?function chgUrl(url) {
? var timestamp = (new Date()).valueOf();
? url = url.substring(0, 17);
? if ((url.indexOf("&") >= 0)) {
? ?url = url + "×tamp=" + timestamp;
? } else {
? ?url = url + "?timestamp=" + timestamp;
? }
? return url;
?}
?function refresh(obj) {
? ? ? ? obj.src = "code.html";
? ? }
</script>
? </head>
??
? <body>?
? ? ? ? 驗證碼<input id="index_code" name="code" type="text" />?
? ? ? ? <img id="imgObj" alt="驗證碼" src="code.html" οnclick="javascript:changeImg();" />?
? ? ? ? <!-- <a href="" οnclick="changeImg()"οnclick="javascript:refresh(this);">換一張</a> -->
? ? ? ? <input type="button" οnclick="javascript:changeImg();" value="看不清換一張">
? ? ? ? <input type="hidden" value="123456" id="code"/>
? </body>
</html>
后臺
/**
?* 用戶驗證碼
?* @author Administrator
?*
?*/
@Controller
public class CodeController {
private int width = 90;//定義圖片的width ?
? ?private int height = 20;//定義圖片的height ?
? ?private int codeCount = 4;//定義圖片上顯示驗證碼的個數 ?
? ?private int xx = 15;?
? ?private int fontHeight = 18;?
? ?private int codeY = 16;?
? ?char[] codeSequence = {?
? ? 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', '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', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
? ? '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };?
?
? ?@RequestMapping("/code")?
? ?public void getCode(HttpServletRequest req, HttpServletResponse resp)?
? ? ? ? ? ?throws IOException {?
?
? ? ? ?// 定義圖像buffer ?
? ? ? ?BufferedImage buffImg = new BufferedImage(width, height,?
? ? ? ? ? ? ? ?BufferedImage.TYPE_INT_RGB);?
? ? ? ?//Graphics2D gd = buffImg.createGraphics(); ?
? ? ? ?//Graphics2D gd = (Graphics2D) buffImg.getGraphics(); ?
? ? ? ?Graphics gd = buffImg.getGraphics();?
? ? ? ?// 創建一個隨機數生成器類 ?
? ? ? ?Random random = new Random();?
? ? ? ?// 將圖像填充為白色 ?
? ? ? ?gd.setColor(Color.WHITE);?
? ? ? ?gd.fillRect(0, 0, width, height);?
?
? ? ? ?// 創建字體,字體的大小應該根據圖片的高度來定。 ?
? ? ? ?Font font = new Font("Fixedsys", Font.BOLD, fontHeight);?
? ? ? ?// 設置字體。 ?
? ? ? ?gd.setFont(font);?
?
? ? ? ?// 畫邊框。 ?
? ? ? ?gd.setColor(Color.BLACK);?
? ? ? ?gd.drawRect(0, 0, width - 1, height - 1);?
?
? ? ? ?// 隨機產生40條干擾線,使圖象中的認證碼不易被其它程序探測到。 ?
? ? ? ?gd.setColor(Color.BLACK);?
? ? ? ?for (int i = 0; i < 40; i++) {?
? ? ? ? ? ?int x = random.nextInt(width);?
? ? ? ? ? ?int y = random.nextInt(height);?
? ? ? ? ? ?int xl = random.nextInt(12);?
? ? ? ? ? ?int yl = random.nextInt(12);?
? ? ? ? ? ?gd.drawLine(x, y, x + xl, y + yl);?
? ? ? ?}?
?
? ? ? ?// randomCode用于保存隨機產生的驗證碼,以便用戶登錄后進行驗證。 ?
? ? ? ?StringBuffer randomCode = new StringBuffer();?
? ? ? ?int red = 0, green = 0, blue = 0;?
?
? ? ? ?// 隨機產生codeCount數字的驗證碼。 ?
? ? ? ?for (int i = 0; i < codeCount; i++) {?
? ? ? ? ? ?// 得到隨機產生的驗證碼數字。 ?
? ? ? ? ? ?String code = String.valueOf(codeSequence[random.nextInt(62)]);?
? ? ? ? ? ?// 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。 ?
? ? ? ? ? ?red = random.nextInt(255);?
? ? ? ? ? ?green = random.nextInt(255);?
? ? ? ? ? ?blue = random.nextInt(255);?
?
? ? ? ? ? ?// 用隨機產生的顏色將驗證碼繪制到圖像中。 ?
? ? ? ? ? ?gd.setColor(new Color(red, green, blue));?
? ? ? ? ? ?gd.drawString(code, (i + 1) * xx, codeY);?
?
? ? ? ? ? ?// 將產生的四個隨機數組合在一起。 ?
? ? ? ? ? ?randomCode.append(code);?
? ? ? ?}?
? ? ? ?// 將四位數字的驗證碼保存到Session中。 ?
? ? ? ?HttpSession session = req.getSession();?
? ? ? ?System.out.println(randomCode);?
? ? ? ?session.setAttribute("code", 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();?
? ?}?
/**********************************調到驗證碼**************************************/
@RequestMapping("/tocode")
public ModelAndView tologin(HttpServletRequest request,HttpServletResponse response) {
ModelAndView mav = new ModelAndView();
mav.setViewName("/user/code");
return mav;
}
}
總結
以上是生活随笔為你收集整理的Spring MVC 验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring mvc学习(23):ecl
- 下一篇: coreseek mysql_cores