JavaWeb 使用Session實(shí)現(xiàn)一次性驗(yàn)證碼功能
表單
<form action="loginServlet" method="post"> 請輸入驗(yàn)證碼:<input type="text" name="code" /> <img src="getCodeServlet" /><br /> <button type="submit">提交</button> </form>
載入頁面時(shí),會(huì)自動(dòng)請求getCodeServlet,獲取圖片(驗(yàn)證碼)。
getCodeServlet,產(chǎn)生驗(yàn)證碼
@WebServlet("/getCodeServlet")
public class GetCodeServlet extends HttpServlet {
//驗(yàn)證碼的寬、高
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);
//隨機(jī)繪制100個(gè)干擾點(diǎn)
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);
//干擾點(diǎn)的顏色也可以隨機(jī),隨機(jī)產(chǎn)生red,green,blue即可
//g.setColor(new Color(red,green,blue));
}
}
//繪制驗(yàn)證碼
private void drawCode(Graphics g,char[] code){
g.setColor(Color.BLACK);
//字體、樣式(多個(gè)時(shí)豎線分隔)、字號(hào)
g.setFont(new Font("serif",Font.ITALIC|Font.BOLD,18));
//在不同位置繪制驗(yàn)證碼字符,參數(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);
}
//隨機(jī)產(chǎn)生4位驗(yàn)證碼
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();
//將驗(yàn)證碼放到session域中。session對(duì)象要在提交響應(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("驗(yàn)證碼正確");
}
else {
response.getWriter().write("驗(yàn)證碼錯(cuò)誤");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
上面的處理方式要區(qū)分驗(yàn)證碼的大小寫。
不區(qū)分大小寫:
//先轉(zhuǎn)換為全大寫|全小寫,再判斷
trueCode=trueCode.toLowerCase();
code=code.toLowerCase();
//trueCode=trueCode.toUpperCase();
//code=trueCode.toUpperCase();
總結(jié)
以上所述是小編給大家介紹的JavaWeb 使用Session實(shí)現(xiàn)一次性驗(yàn)證碼功能,希望對(duì)大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
在IDEA中配置tomcat并創(chuàng)建tomcat項(xiàng)目的圖文教程
這篇文章主要介紹了在IDEA中配置tomcat并創(chuàng)建tomcat項(xiàng)目的圖文教程,需要的朋友可以參考下2020-07-07
關(guān)于java開發(fā)的性能問題總結(jié)(必看)
下面小編就為大家?guī)硪黄P(guān)于java開發(fā)的性能問題總結(jié)(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
在Java生產(chǎn)環(huán)境下進(jìn)行性能監(jiān)控與調(diào)優(yōu)的詳細(xì)過程
在Java生產(chǎn)環(huán)境下進(jìn)行性能監(jiān)控與調(diào)優(yōu)是一個(gè)復(fù)雜但重要的過程,它涉及到多個(gè)方面,包括代碼分析、JVM監(jiān)控、線程管理、垃圾收集優(yōu)化、內(nèi)存管理、數(shù)據(jù)庫交互等,下面我將提供一個(gè)詳細(xì)的概述和示例代碼,需要的朋友可以參考下2025-02-02

