JSP+servlet產生驗證碼並驗證,jspservlet

來源:互聯網
上載者:User

JSP+servlet產生驗證碼並驗證,jspservlet



產生驗證碼的基本過程是:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import="java.awt.*,java.awt.image.BufferedImage,javax.imageio.ImageIO" %>     <%      response.setHeader("Cache-Control", "no-cache");      int width=60,height=20;      <!--執行個體化bufferdImage對象-->      BufferedImage image=new BufferedImage( width,height,BufferedImage.TYPE_INT_RGB);      //由bufferdImage對象獲得畫筆g      Graphics g=image.getGraphics();      //畫筆填充矩形      g.setColor(new Color(200,200,200));      g.fillRect(0, 0, width, height);      //產生隨機數      Random rd=new Random();      int rdNum=rd.nextInt(8999)+1000;      String rdstr=String.valueOf(rdNum);      //把隨機數放入session中      session.setAttribute("rdStr", rdstr);      //畫筆g畫隨機數      g.setColor(Color.BLUE);      g.setFont(new Font("",Font.PLAIN,20));      g.drawString(rdstr, 10, 20);//這裡的座標是一個字元的左下角基準         //畫100個隨機點      g.setColor(Color.green);      for(int i=0;i<100;i++){           int x=rd.nextInt(width);           int y=rd.nextInt(height);           g.drawOval(x, y, 1, 1);      }      //用ImageIO的靜態函數write輸出映像      ImageIO.write(image, "JPEG", response.getOutputStream());      out.clear();      out=pageContext.popBody();      %>

畫圖的基本思想是:

<!--loginform.jsp登入jsp--><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'loginform.jsp' starting page</title>       <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>   <body>        <script type="text/javascript">          function refresh(){                document.loginform.imgcode.src="codes.jsp";          }        </script>          <form name="loginform" action="/code/servlets/codeServlet" method="post">        <h4 align="center"><b>歡迎登陸</b></h4><br>                <table align="center" border="1">         <tr>            <td align="right">請輸入帳號:</td>            <td><input name="zhanghao" type="text" > </td>         </tr>         <tr>            <td align="right">請輸入密碼:</td>            <td><input name="mima" type="password" size="21" > </td>         </tr>         <tr>            <td align="right">請輸入驗證碼:</td>            <td ><input name="yanzhengma" type="text" size="10" ><img  name="imgcode" src="codes.jsp" onclick="refresh()"> </td><!--驗證碼當圖片處理,點擊可重新整理-->         </tr>         <tr>            <td></td>            <td><input type="submit" value="登入" > <input type="button" value="忘記密碼?"></td>         </tr>       </table>               </form>  </body></html>

<!--codeServlet.java-->package servlets;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class codeServlet extends HttpServlet {    /**     * The doPost method of the servlet. <br>     *     * This method is called when a form has its tag value method equals to post.     *      * @param request the request send by the client to the server     * @param response the response send by the server to the client     * @throws ServletException if an error occurred     * @throws IOException if an error occurred     */    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //擷取表單的驗證碼        String code=request.getParameter("yanzhengma");          //擷取session中的驗證碼          HttpSession session=request.getSession();          String randstr=(String) session.getAttribute("rdStr");          response.setCharacterEncoding("gb2312");//respons設為中文編碼          PrintWriter out=response.getWriter();//獲得輸出寫出器           if(code.equals(randstr)){//輸入的驗證碼和session儲存的驗證碼相比較                    out.println("恭喜你,驗證碼正確!");                    }           else{                               //response.sendRedirect("/code/back.jsp");               out.println("驗證碼錯誤!!");           }              }}

輸入結果是:

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.