導航:首頁 > 編程語言 > jsp驗證碼不顯示

jsp驗證碼不顯示

發布時間:2023-10-27 23:33:31

jsp中生成的驗證碼不能正確顯示

http://..com/question/582353044.html?oldq=1&from=evaluateTo#reply-box-1465393158

㈡ 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();
}

}

㈢ jsp生成的驗證碼圖片怎麼顯示不出來,總是一個X框的東西,哪位高手指教下!

可以在X的位置,右鍵屬性,看看對應的url是否正確,這樣,你首先可以直接訪問url,然後,如果正常顯示,再放到img標簽里,就ok了,還問題,可以提出來,我幫你解決

㈣ 在我的java Web項目中為什麼我的jsp的驗證碼不能顯示啊其他項目可以顯示就是我的這一個項目不能顯示。

如果其他可以,這個不行,你可以試著重啟你的伺服器,清除瀏覽器緩存再打開試試..

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

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

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

閱讀全文

與jsp驗證碼不顯示相關的資料

熱點內容
用於建立或修改程序文件的命令是 瀏覽:25
phantomjs指定埠 瀏覽:448
extjs獲取當前第幾行 瀏覽:518
怎樣導出sql資料庫文件格式 瀏覽:157
u盤系統文件格式 瀏覽:376
javafind數組 瀏覽:41
t2080升級cpu 瀏覽:190
微信文件倒出來哪裡找到 瀏覽:722
單元格刪除文件夾內容 瀏覽:170
word2003如何修改作者 瀏覽:217
默認資料庫埠 瀏覽:537
sql資料庫mdb在哪裡 瀏覽:819
win10創作者更新了什麼好處 瀏覽:994
linuxtunelp 瀏覽:665
centos打開zip文件格式 瀏覽:995
java文明5 瀏覽:86
ipad輸密碼沒反應 瀏覽:916
一號文件中提出了哪些要求 瀏覽:527
scl用什麼語言編程 瀏覽:962
系統前台進行的程序 瀏覽:270

友情鏈接