導航:首頁 > 編程語言 > 純jsp驗證碼刷新

純jsp驗證碼刷新

發布時間:2023-05-26 15:56:05

1. jsp頁面該如何刷新驗證碼

(1)jsp代碼
<img id = "img_authcode" src="${ctx}/account/authcode" /><a href="javascript:;" onclick="javascript:document.getElementById('img_authcode').setAttribute('src', '${ctx}/account/authcode?' + Math.random())">換一換</a>

(2)java代碼(該代碼為我自己框架代碼,跟servlet寫法不一樣的我都給你注釋了):
public View authcode() throws IOException {
HttpServletResponse response = PuffContext.getResponse();//獲取response
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
String authCode = AuthCodeUtil.getRandom(4); //獲取驗證碼,代碼在下面(3)
System.out.println("生成隨機碼:" + authCode);
PuffContext.getSession().setAttribute("session_authcode", authCode);//把該驗證碼存儲在session
ServletOutputStream output = response.getOutputStream();
AuthCodeUtil.draw(output, authCode);
output.flush();
output.close();
return ViewFactory.nullView();//返回null
}

(3)///////////////////////////下面為生成驗證碼類////////////////////////////////////

public class AuthCodeUtil {
private final static Random random = new Random();
// 隨機字體樣式
private final static int[] fontStyle = { Font.HANGING_BASELINE, Font.ITALIC, Font.LAYOUT_LEFT_TO_RIGHT, Font.LAYOUT_NO_LIMIT_CONTEXT,
Font.LAYOUT_NO_START_CONTEXT, Font.LAYOUT_RIGHT_TO_LEFT };

/**
* 畫隨機碼圖
*
* @param out
* @param width
* @param height
* @throws IOException
*/
public static void draw(OutputStream out, String value) throws IOException {
int width = 80, height = 30;
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
g.drawRect(1, 1, width - 2, height - 2);
for (int i = 0; i < 10; i++) {
g.setColor(randColor(150, 250));
g.drawOval(random.nextInt(110), random.nextInt(24), 5 + random.nextInt(10), 5 + random.nextInt(10));
}
int n = (int) (Math.random() * 6);
Font mFont = new Font("Arial", fontStyle[n], 23);
g.setFont(mFont);
g.setColor(randColor(10, 240));
g.drawString(value, 10, 21);// 隨機數,水平距離,垂直距離
ImageIO.write(bi, "png", out);
}

private static Color randColor(int fc, int bc) {// 給定范圍獲得隨機顏色
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("d:\\aa.png");
draw(out, getRandom(4));
}

public static String getRandom(int size) {// 隨機字元串
char[] c = { '1', '3', '5', '6', '7', '8', '9' };
StringBuffer sb = new StringBuffer();
for (int i = 0; i < size; i++)
sb.append(c[Math.abs(random.nextInt()) % c.length]);
return sb.toString();
}

}

2. jsp 一段點擊驗證碼刷新的代碼,看不太懂

這就是一張圖片,圖片引用了一個JSP頁面,image.jsp生成的驗證碼顯示在這個頁面的圖片上,seeE方法就起到了刷新驗證碼的作用,把路徑重新賦值給圖片就是刷新效果,後面的時間是因為javascript的緩存機制引起的。你要是用純代碼來寫Ajax無刷新登錄就會知道原理。相同的路徑不會向後台發出第二次請求,時間的毫秒數是不可能重復的,所以每次路徑都不一樣。另外有時候還可以用隨機數

3. 怎麼ajax實現jsp的驗證碼部分刷新

<script language="javascript">
function loadimage(){
document.getElementById("randImage").src = "image.jsp?"+Math.random();
}
</script>
<img alt="code..." name="randImage" id="randImage" src="image.jsp" border="基褲檔1" >
image.jsp
<%@ page contentType="image/jpeg;charset=GBK" import="java.awt.*, java.awt.image.*,java.util.*,javax.imageio.*" %>
<%@ page import="java.io.OutputStream" %>
<搏亂%!
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);
int b=fc+random.nextInt(bc-fc);
return new Color(r,g,b);
}
%>
<%
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=80, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
String sRand="";
for (int i=0;i<6;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
g.drawString(rand,13*i+6,16);
}
// 將認純檔證碼存入SESSION
session.setAttribute("rand",sRand);
g.dispose();
OutputStream os=response.getOutputStream();

ImageIO.write(image, "JPEG", os);

os.flush();
os.close();
os=null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
%>

4. 我用jsp做了一個登錄界面,用另一個jsp做了驗證碼,放在第一個界面當中怎麼才能刷新驗證碼

獲取驗證碼時不用寫在js里敬芹面,你可以這樣寫就能刷新驗證碼了
<img src="image.jsp" style="cursor:pointer" onclick="this.src='code.jsp?t1='+Math.random()" title="看不清?點擊圖片試試">
其中這虛笑句onclick="this.src='code.jsp?t1='+Math.random()" 就是亮譽畢起到刷新作用

5. JSP前台驗證碼做驗證時session值總是記住刷新前的一次,求大神指導!!!

你在產生圖片後就更新下session驗證碼的值就可以啦

6. JSP頁面驗證碼不能刷新怎麼回事

鏈接後面帶個隨機數參數試試
xxx.jsp?a=Math.random()

7. 用Js,Ajax做的一個jsp頁面的驗證碼功能,但是就是刷不出來圖片呀,總是一把×。查了好久,還是沒有解決。

你的圖片路徑 你確定是 code?code=隨機數 么?
你這不是發送到後台的 地址? 確定是 圖片的絕對(相版對) 地址?

你犯了一個權錯誤。 $('imgVcode') 這個應該是你的圖片的id 。 你想著給他 賦值 .src
這是對的。
然而應該先 ajax 請求 得到 一個隨機數再 把後台返回過來的隨機數 賦值吧?
例如這樣$ajax{
url:
type:
data:
} success : function(result){
$('imgVcode').src="code+result";
}
應該是這樣吧。

8. JSP驗證碼刷新報錯:java.net.SocketException: Connection reset by peer: socket write error

太奇怪,我測試了一下你的代碼,很好用阿,採用兩種方式都沒有問題

直接訪問img.jsp或者訪問html文件的img標簽基含,都沒有搏談笑問題。

補充:

我是在本地訪問侍拆的,如果你訪問其它計算機,那麼看看是否是防火牆的問題。代碼應該沒有問題。

9. jsp下面刷新驗證碼 代碼如下document.getElementById("authImg").src="authImgnow="+new Date();

document.getElementById("authImg").src="authImg?now="+new Date(); 先改成:
document.getElementById("authImg").src="aa.jpg"然後在IE7、IE6和IE8下試試,如果可以的話就說明版你後面的路徑問題,如果路長沒權有問題,你再在IE7和IE6下alert(new Date());看能否彈出對話框。

10. java web 項目驗證碼的刷新問題

你可以來用js來更新源img的src屬性,例如
<img src="PicktureCheckCode" id="CreateCheckCode" align="middle" onclick="this.src=this.src+'?'" />
<a href="javascript:CreateCheckCode.onclick()">看不清,換一張</a>

閱讀全文

與純jsp驗證碼刷新相關的資料

熱點內容
不用網路載入的單機游戲有哪些 瀏覽:608
數據線插頭怎麼接頭 瀏覽:577
網路載入視頻失敗是怎麼回事 瀏覽:805
傳奇賬號在哪個文件夾里 瀏覽:346
百度app在哪裡安裝 瀏覽:587
如何設置路由器網路不斷網 瀏覽:471
傳到qq群里的文件怎麼刪除 瀏覽:861
索尼安卓71更新日誌 瀏覽:234
怎麼找手機里的垃圾app 瀏覽:540
2015藍橋杯代碼填空 瀏覽:698
安卓資料庫dbexecSQL 瀏覽:227
doc重命名文件格式 瀏覽:728
getscreen截圖工具下載 瀏覽:719
共識數據是什麼時候開始的 瀏覽:96
數碼管顯示電壓程序 瀏覽:479
資料庫文件有哪個 瀏覽:543
途強儲存在哪個文件夾 瀏覽:172
如何恢復被覆蓋文件 瀏覽:611
iphone5用哪個版本最好 瀏覽:327
extjsgrid禁用 瀏覽:426

友情鏈接