javaweb驗證碼實現(jsp)

首先放上javaweb的目錄結構:css

生成驗證碼的jsp文件【validate.jsp】java

 

<%
	response.setHeader("Cache-Control", "no-cache");
	int width=60,height=20;
	BufferedImage bufferedImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
	Graphics graphics=bufferedImage.getGraphics();
	graphics.setColor(new Color(200,200,200));
	graphics.fillRect(0, 0, width, height);
	Random random=new Random();
	int randnum=random.nextInt(8999)+1000;
	String ranString=String.valueOf(randnum);
	session.setAttribute("randStr", ranString);
	graphics.setColor(Color.BLACK);
	graphics.setFont(new Font("",Font.PLAIN,20));
	graphics.drawString(ranString, 10, 17);
	for(int i=0;i

生成以後直接放在session裏面,也是爲了後面提交以後的驗證(這篇暫時不寫驗證)其中值得一提的是,這個不能直接訪問,要是直接訪問就是一堆亂碼在瀏覽器上上顯示出來,因此咱們要用標籤來把它顯示出來,顯示頁面以下:web

 

【lohinFrom】瀏覽器

 

Insert title herelogin
	歡迎登陸 
           	
           	
				
			

其中使用到【login..css】文件session

 

 

.login{
	padding:20px;
	margin: 0 auto;
	width:200px;
	height:300px;
	border:1px solid #F00;
}
.message{
	magin:0 auto;
	text-align:center;
}
.loginin{
	margin:0 auto; 
	width:80%;
	display:block;                                                                                    
}
.img_code{
	margin-left:30px;
	margin-top:20px;
}
.box{
	margin-top:30px;
}
.username{
	width:100%;
	margin-top:15px;
}
.password{
	width:100%;
	margin-top:15px;
}
.check_code{
	margin-top:15px;
}

 

顯示結果以下,直接點擊驗證碼能夠刷新,驗證碼刷新的時候,要加一個參數,是爲了保證正常刷新。

 

 

 

驗證驗證碼的正確性【judgevalidate.jsp】dom

 

Insert title here
	

若是有所幫助,臉皮厚求個贊~jsp

此文章僅表明本身(本菜鳥)學習積累記錄,或者學習筆記,若有侵權,請聯繫做者刪除。人無完人,文章也同樣,文筆稚嫩,在下不才,勿噴,若是有錯誤之處,還望指出,感激涕零~ide

技術之路不在一時,山高水長,縱使緩慢,馳而不息。學習

公衆號:秦懷雜貨店3d

相關文章
相關標籤/搜索