WEB——Session案例一之驗證碼

實現一個如下的驗證碼的案例;

這裏寫圖片描述

大致思路如下:
這裏寫圖片描述

需要的jar包可以通過如下網址獲取:http://download.csdn.net/detail/panpan_1994/9621655

login.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 'index.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">
    -->
    <script type="text/javascript">
    function fun(){
        var img= document.getElementById("one");
        img.src="/demo-login/AServlet?date="+new Date();
    }
    </script>
  </head>

  <body>
        <form action="/demo-login/BServlet">
            用戶名:
            <input type="text" name="name" />
            <br>
            密碼:
            <input type="password" name="password" />
            <br>
            驗證碼:
            <input type="text" name="code" size="4" />
            <img id="one"  src="/demo-login/AServlet">
            <a href="javascript:void(0)" onclick="fun();">看不清,換一張</a>
            <font color="red">${requestScope.error }</font>

            <br>
            <input type="submit" value="登錄" />
            </form>
  </body>
</html>

AServlet.java

public class AServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    response.setContentType("img/jpeg");
        ValidateCode code = new ValidateCode(150, 70, 4, 20);
        String str = code.getCode();        request.getSession().setAttribute("code", str);
        System.out.println(str);
    code.write(response.getOutputStream());
    }
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);

    }

}

BServlet.java

public class BServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String code = request.getParameter("code");
        String sessCode = (String) request.getSession().getAttribute("code");
        if (code.equalsIgnoreCase(sessCode)) {

            response.sendRedirect("/demo-login/index.jsp");
        } else {
            request.setAttribute("error", "請重新輸入驗證碼!");
            request.getRequestDispatcher("/login.jsp").forward(request,
                    response);
        }

    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);

    }

}
發佈了40 篇原創文章 · 獲贊 6 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章