国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

Struts2實(shí)現(xiàn)生成動(dòng)態(tài)驗(yàn)證碼并驗(yàn)證實(shí)例代碼

2019-11-26 14:15:28
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

 一、基本流程:

產(chǎn)生一個(gè)驗(yàn)證碼頁(yè)面(很小)→嵌入到表單中→點(diǎn)擊可以刷新頁(yè)面→表單提交時(shí)驗(yàn)證。

二、方法:

1、定義TestAction,實(shí)現(xiàn)畫(huà)圖方法

package com.zhuguang.action; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.util.Map; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.interceptor.SessionAware; import com.opensymphony.xwork2.ActionSupport; import com.sun.image.codec.jpeg.JPEGCodec; import com.sun.image.codec.jpeg.JPEGImageEncoder; public class TestAction extends ActionSupport implements SessionAware,ServletResponseAware { private Map<String, Object> session; private HttpServletResponse response; private static final long serialVersionUID = 1L; private String chknumber; @Override public String execute() throws Exception { response.setHeader("Cache-Control", "no-cache"); int width=50; //圖片寬度 int height=20; //圖片高度 BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics graphics=image.createGraphics(); graphics.setColor(this.getColor()); //背景顏色 graphics.fillRect(0, 0, width, height); graphics.setFont(new Font("Arial",Font.BOLD,18)); graphics.setColor(this.getColor()); //字的顏色 String number=String.valueOf(System.currentTimeMillis()%9000+1000); //生成四位隨機(jī)數(shù) session.put("randomCode", number); //寫(xiě)入session中 graphics.drawString(number, (int)(width*0.1), (int)(height*0.8)); graphics.dispose(); JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(response.getOutputStream()); encoder.encode(image); response.getOutputStream().flush(); //刷新到頁(yè)面生成圖片 response.getOutputStream().close(); //關(guān)閉writer return null; } private Color getColor(){ int red=(int)(Math.random()*1000%256); int green=(int)(Math.random()*1000%256); int blue=(int)(Math.random()*1000%256); return new Color(red,green,blue); } public String getChknumber() { return chknumber; } public void setChknumber(String chknumber) { this.chknumber = chknumber; } @Override public void setSession(Map<String, Object> session) { // TODO Auto-generated method stub this.session = session; } @Override public void setServletResponse(HttpServletResponse response) { // TODO Auto-generated method stub this.response = response; } } 

注意用到session和response

2、在struts.xml文件中注冊(cè):

<action name="randomCode" class="com.zhuguang.action.TestAction"> </action> 

其中不返回任何信息,這樣就不會(huì)跳轉(zhuǎn)頁(yè)面

3、jsp頁(yè)面編寫(xiě)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="/struts-tags" prefix="s" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript"> <!-- function reloadcode(obj,base){ var rand=new Date().getTime(); //這里用當(dāng)前時(shí)間作為參數(shù)加到url中,是為了使URL發(fā)生變化,這樣驗(yàn)證碼才會(huì)動(dòng)態(tài)加載, //只是一個(gè)干擾作用,無(wú)確實(shí)意義,但卻又非常巧妙,呵呵 obj.src=base+"randomCode.action?abc="+rand; //其實(shí)服務(wù)器端是沒(méi)有abc的字段的。 } //--> </script> <title>測(cè)試頁(yè)面</title> </head> <body> <form action="testLogin" method="post"> Username<input type="text" name="name"><br> Password<input type="text" name="password"><br> 驗(yàn)證碼:<input type="text" name="chknumber" id="chknumber" maxlength="4" class="chknumber_input"> <img title="看不清楚請(qǐng)點(diǎn)擊這里" width="50" height="20" src="<%=basePath%>randomCode.action" id="safecode" onclick="reloadcode(this,'<%=basePath%>')" /><br> <input type="submit" value="Loginin"> </form> </body> </html> 

4、驗(yàn)證

(1)在Action中添加一個(gè)驗(yàn)證方法

public String testLogin() { if(session.get("randomCode").equals(chknumber)) { return SUCCESS; } else { return ERROR; } } 

(2)在struts.xml中進(jìn)行注冊(cè)

<action name="testLogin" class="com.zhuguang.action.TestAction" method="testLogin"><result name="success">success.jsp</result><result name="error">error.jsp</result></action>

以上所述是小編給大家介紹的Struts2實(shí)現(xiàn)生成動(dòng)態(tài)驗(yàn)證碼并驗(yàn)證實(shí)例代碼,希望對(duì)大家有所幫助!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宝山区| 莆田市| 乌鲁木齐县| 大英县| 正定县| 杭锦后旗| 佛教| 延津县| 敦煌市| 赣州市| 玛多县| 新巴尔虎左旗| 达拉特旗| 长泰县| 蓬溪县| 定远县| 宁武县| 调兵山市| 申扎县| 大渡口区| 铜陵市| 浪卡子县| 苍南县| 汉中市| 博客| 曲沃县| 遵化市| 威宁| 循化| 集贤县| 兴化市| 淮北市| 宝坻区| 平利县| 安庆市| 应城市| 阳春市| 黑河市| 新余市| 许昌市| 诸城市|