JavaWeb 使用Session實現(xiàn)一次性驗證碼功能
表單
<form action="loginServlet" method="post"> 請輸入驗證碼:<input type="text" name="code" /> <img src="getCodeServlet" /><br /> <button type="submit">提交</button> </form>
載入頁面時,會自動請求getCodeServlet,獲取圖片(驗證碼)。
getCodeServlet,產(chǎn)生驗證碼
@WebServlet("/getCodeServlet")
public class GetCodeServlet extends HttpServlet {
//驗證碼的寬、高
private static int WIDTH=80;
private static int HEIGHT=25;
//繪制背景
private void drawBg(Graphics g){
//rgb
g.setColor(new Color(128, 128, 128));
//繪制矩形。x,y,wigth,height
g.fillRect(0,0,WIDTH,HEIGHT);
//隨機繪制100個干擾點
Random random=new Random();
for (int i=0;i<100;i++){
//產(chǎn)生(0,1)上的小數(shù),*WIDTH|HEIGHT,再取整也行
int x=random.nextInt(WIDTH);
int y=random.nextInt(HEIGHT);
g.drawOval(x,y,1,1);
//干擾點的顏色也可以隨機,隨機產(chǎn)生red,green,blue即可
//g.setColor(new Color(red,green,blue));
}
}
//繪制驗證碼
private void drawCode(Graphics g,char[] code){
g.setColor(Color.BLACK);
//字體、樣式(多個時豎線分隔)、字號
g.setFont(new Font("serif",Font.ITALIC|Font.BOLD,18));
//在不同位置繪制驗證碼字符,參數(shù):要繪制的String、橫、縱坐標(biāo)。+""是為了char轉(zhuǎn)String。
g.drawString(code[0]+"",1,17);
g.drawString(code[1]+"",16,15);
g.drawString(code[2]+"",31,18);
g.drawString(code[3]+"",46,16);
}
//隨機產(chǎn)生4位驗證碼
private char[] getCode(){
String chars="0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm";
char[] code=new char[4];
Random random=new Random();
for (int i=0;i<4;i++){
//[0,62)
int index= random.nextInt(62);
code[i]=chars.charAt(index);
}
return code;
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
ServletOutputStream sos = response.getOutputStream();
response.setContentType("image/jpeg");
//設(shè)置瀏覽器不緩存此圖片
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires",0);
//創(chuàng)建內(nèi)存圖片
BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, TYPE_INT_RGB);
Graphics g= bufferedImage.getGraphics();
char[] code=getCode();
//將驗證碼放到session域中。session對象要在提交響應(yīng)之前獲得
session.setAttribute("code",new String(code));
drawBg(g);
drawCode(g,code);
g.dispose();
//將圖片輸出到瀏覽器
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bufferedImage,"JPEG",baos);
baos.writeTo(sos);
baos.close();
sos.close();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
doPost(request,response);
}
}
loginServlet,處理表單
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
HttpSession session = request.getSession();
String trueCode= (String) session.getAttribute("code");
String code=request.getParameter("code");
if (code.equals(trueCode)){
response.getWriter().write("驗證碼正確");
}
else {
response.getWriter().write("驗證碼錯誤");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
上面的處理方式要區(qū)分驗證碼的大小寫。
不區(qū)分大小寫:
//先轉(zhuǎn)換為全大寫|全小寫,再判斷
trueCode=trueCode.toLowerCase();
code=code.toLowerCase();
//trueCode=trueCode.toUpperCase();
//code=trueCode.toUpperCase();
總結(jié)
以上所述是小編給大家介紹的JavaWeb 使用Session實現(xiàn)一次性驗證碼功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
SpringBoot整合EasyExcel進行大數(shù)據(jù)處理的方法詳解
EasyExcel是一個基于Java的簡單、省內(nèi)存的讀寫Excel的開源項目。在盡可能節(jié)約內(nèi)存的情況下支持讀寫百M的Excel。本文將在SpringBoot中整合EasyExcel進行大數(shù)據(jù)處理,感興趣的可以了解一下2022-05-05
java環(huán)境配好后jar文件打開命令框閃退(無打開方式,無反應(yīng))解決辦法
在Java開發(fā)中我們經(jīng)常會遇到運行Jar包時閃退的問題,下面這篇文章主要給大家介紹了關(guān)于java環(huán)境配好后jar文件打開命令框閃退(無打開方式,無反應(yīng))的解決辦法,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2024-04-04
SpringBoot實現(xiàn)Read Through模式的操作過程
Read Through模式通常是指一種緩存策略,其中當(dāng)應(yīng)用程序嘗試讀取數(shù)據(jù)時,緩存系統(tǒng)首先被檢查以查看數(shù)據(jù)是否已經(jīng)存在于緩存中,這篇文章主要介紹了SpringBoot實現(xiàn)Read Through模式,需要的朋友可以參考下2024-07-07
java Spring Boot 配置redis pom文件操作
這篇文章主要介紹了java Spring Boot 配置redis pom文件操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Springboot中MyBatisplus使用IPage和Page分頁的實例代碼
這篇文章主要介紹了Springboot中MyBatisplus使用IPage和Page分頁,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12
Springboot自動配置原理及DataSource的應(yīng)用方式
這篇文章主要介紹了Springboot自動配置原理及DataSource的應(yīng)用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-07-07
使用mybatisplus接收mysql字段為json類型的數(shù)據(jù)方式
這篇文章主要介紹了使用mybatisplus接收mysql字段為json類型的數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-04-04

