進行一次性校驗碼的校驗(session、時間戳、隨機數、字元快取、繪畫)
阿新 • • 發佈:2019-02-08
<!DOCTYPE html> <html> <head> <title>MyHtml.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="this is my page"> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript"> function changeimage(){ document.getElementById("imag").src="/J2EE/checkimagservlet?time="+new Date().getTime(); //由於沒有改變路徑,瀏覽器有快取,自動載入快取內的值,加上時間戳後每次路徑不一樣 } </script> </head> <body> <form action="/J2EE/myhttpservlet" method="post"> 姓名 <input name="username" type="text"> 密碼 <input name="password" type="password"> <div> 驗證碼<input name="code" type="text"> </div> <div> <img src="/J2EE/checkimagservlet" id="imag" onclick="changeimage()"> </div> <!-- //驗證碼生成的servlet --> <input type="submit" name="提交"> </form> </body> </html>
package javapack; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.IOException; import java.io.PrintWriter; import java.util.Random; import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class checkimagservlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int width=120; int height=30; //步驟一 繪製一張記憶體中的圖片 BufferedImage bi=new BufferedImage (width,height,BufferedImage.TYPE_INT_RGB);//表示一個影象,該影象具有整數畫素的 8 位 RGB 顏色 //步驟二 圖片繪製背景顏色 ---通過繪圖物件 Graphics g=bi.getGraphics();//得到畫圖物件---畫筆 //繪製任何圖形之前,都必須指定一個顏色 g.setColor(getRandColor(200,250)); g.fillRect(0, 0, width, height);//畫填充矩形,前兩個引數指的是矩形左上角的座標,後兩個引數是矩形的寬和高 //步驟三 繪製邊框 g.setColor(Color.WHITE); g.drawRect(0, 0, width-1, height-1); //步驟四 四個隨機數字 Graphics2D g2=(Graphics2D) g; //設定輸出字型 g2.setFont(new Font("宋體",Font.BOLD,18));//引數為字型、風格、字號 String words="ABCDEFGHIGKLMNOPQRSTUVWXYZ1234567890"; Random random=new Random();//生成隨機數 //將生成的驗證碼儲存到session中 StringBuffer buffer=new StringBuffer(); //定義X座標 int x=10; for(int i=0;i<4;i++){ //隨機顏色new corlor(r,g,b) g2.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//nextInt(n),生成一個0到n的數 //旋轉-30,---30度 int jiaodu =random.nextInt(60)-30; //換算弧度 double theta =jiaodu*Math.PI/180; //生成一個隨機數字 int index=random.nextInt(words.length());//從word中隨便選取一個值 //獲得字母數字 char c=words.charAt(index); //將生成的字加入到buffer中 buffer.append(c); //將c輸出到圖片 g2.rotate(theta, x, 20); g2.drawString(String.valueOf(c), x, 20); g2.rotate(-theta, x, 20); x+=30; } request.getSession().setAttribute("code", buffer.toString()); //步驟五 繪製干擾線 g.setColor(getRandColor(160,200)); int x1; int x2; int y1; int y2; for(int i=0;i<30;i++){ x1=random.nextInt(width); x2=random.nextInt(12); y1=random.nextInt(height); y2=random.nextInt(12); g.drawLine(x1, y1, x1+x2, y1+y2); } //將上面圖片輸出到瀏覽器 g.dispose();//釋放資源 ImageIO.write(bi,"jpg",response.getOutputStream()); } private Color getRandColor(int fc, int bc) { // TODO Auto-generated method stub 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); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package javapack; import java.io.IOException; import java.io.PrintWriter; import java.sql.SQLException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class httpservlet extends HttpServlet { @Override public void init() throws ServletException { // TODO Auto-generated method stub //初始化一個變數count值為0,用來記錄登入的人數 int count=0; //將這個值存入servletcontext this.getServletContext().setAttribute("count", count); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8");//解決向頁面輸出中文亂碼,而且還可以寫HTML final long serialVersionUID=1L; //校驗驗證碼 String code1 = request.getParameter("code"); String code2 = (String) request.getSession().getAttribute("code"); request.getSession().removeAttribute("code");//清除session if(!code1.equalsIgnoreCase(code2))//忽略大小寫 { request.setAttribute("msg", "驗證碼輸入錯誤"); request.getRequestDispatcher("/html/login.jsp").forward(request,response); return; } //接收表單引數 String username=request.getParameter("username");//通過input的name值獲得輸入的username String password=request.getParameter("password"); //封裝到實體物件 User user=new User(); user.setUsername(username);//將username與password封裝到user物件中 user.setPassword(password); //呼叫業務層處理資料 userservice us=new userservice(); try { User existuser=us.login(user);//將user傳入到login方法判斷使用者是否在資料庫中存在 //根據處理結果顯示資訊(頁面跳轉) if(existuser==null){ response.getWriter().println("<h1>登入失敗</h1>"); } else{ //記錄登入成功的人數 int count=(Integer) this.getServletContext().getAttribute("count"); count++; this.getServletContext().setAttribute("count", count); /*response.getWriter().println("<h1>登入成功,你好,"+existuser.getUsername()+"</h1></br>"); response.getWriter().println("<h1>1s後頁面跳轉。。。。</h1>"); response.setHeader("Refresh","1;url=/J2EE/countservlet");*/ /*response.setStatus(302);//狀態碼重定向 response.setHeader("Location", "/J2EE/html/success.html");*/ response.sendRedirect( "/J2EE/countservlet"); return; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);//post方式和get方式執行同一方法 } }