【笔记】验证码的使用

【页面】

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
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>艾特|后台登录</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="Admin/Css/login.css">
    <script type="text/javascript" src="Admin/Js/login.js"></script>
    <script type="text/javascript">
        function changeCode(obj){
            obj.src="code?id="+Math.random();
        }
        function changex(obj){
            obj.style.cursor="pointer";
        }
    </script>
</head>
<body>

        <%
            Cookie[] cookies = request.getCookies(); 
             if(cookies!=null){
                for(Cookie cookie : cookies){
                    if(cookie.getName().equals("myNR")){
                        String[] str=cookie.getValue().split(":");
                        session.setAttribute("athl_name", str[0]);
                        session.setAttribute("athl_role", Integer.valueOf(str[1]));
                        response.sendRedirect("index.jsp");
                        return;
                    }
                }
            } 
            String msg=(String)request.getAttribute("msg"); 
        %>
        <div id="login_frame">
            <div class="headPhoto"><img src="${pageContext.request.contextPath }/Admin/Images/head.jpg"></div>
            <span id="hint">
            <c:if test="${not empty msg }">
            <c:out value="${msg }"></c:out>
            </c:if>
            </span>
            <form id="login" class="login_form" action="login"  method="post">
                <div class="login_input_panel">
                    <div class="login_input">
                        <i class="icon-user"></i>
                        <input type="text" placeholder="账号" id="username" name="username"/>
                    </div>
                    <div class="login_input">
                        <i class="icon-lock" style="left: 8%"></i>
                        <input type="password" placeholder="密码" id="pwd" name="password" />
                    </div>
                    <div class="login_input">
                        <i class="icon-barcode"></i>
                        <input type="text" placeholder="验证码" name="code"/> 
                    </div>
                    <div class="mycookie">
                        <label> 
                            <input name="mycookie" class="cooinput"  type="checkbox" value="1">自动登录
                        </label>
                        <label> 
                        <img src="code" id="code" onmouseover="changex(this)" onclick="changeCode(this)" alt="点击更换" title="点击更换" />
                        </label>
                    </div>
                </div>

                <div class="login_btn">
                    <input type="submit" id="login" class="submit" value="登录"/>
                </div>
            </form>
        </div>
  </body>
</html>

【servlet】

package com.athl.servlet;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.athl.dao.AdminDao;
import com.athl.dao.RolesDao;

public class AdminServlet extends HttpServlet {


    private static final long serialVersionUID = 1L;

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

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String url = request.getRequestURI().toString();
        if (url.endsWith("login")) {
            this.login(request, response);
        } else if (url.endsWith("logout")) {
            this.logout(request, response);
        } else if (url.endsWith("getAdminAll")) {
            this.getAdminAll(request, response);
        } else if (url.endsWith("delete")) {
            this.delete(request, response);
        } else if (url.endsWith("unblock")) {
            this.unblock(request, response);
        } else if (url.endsWith("block")) {
            this.block(request, response);
        } else if (url.endsWith("addAdmin")) {
            this.addAdmin(request, response);
        } else if (url.endsWith("updateAdmin")) {
            this.updateAdmin(request, response);
        } else if (url.endsWith("initAddAdmin")) {
            this.initAddAdmin(request, response);
        } else if (url.endsWith("initUpdateAdmin")) {
            this.initUpdateAdmin(request, response);
        }
    }

    /**
     * 初始化管理员修改页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void initUpdateAdmin(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        int adId=Integer.valueOf(request.getParameter("id"));

        List<Map<String,Object>> lmr =new RolesDao().getRolesAll();

        request.setAttribute("adId", adId);
        request.setAttribute("lmr", lmr);
        //请求转发
        request.getRequestDispatcher("Admin/updateAdmin.jsp").forward(request, response);
    }

    /**
     * 初始化管理员添加页面
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void initAddAdmin(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        List<Map<String,Object>> lmr =new RolesDao().getRolesAll();
        request.setAttribute("lmr", lmr);
        request.getRequestDispatcher("Admin/addAdmin.jsp").forward(request, response);
    }

    /**
     * 修改
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void updateAdmin(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int adId=Integer.valueOf(request.getParameter("adId"));
        String name=request.getParameter("adName");
        String pwd=request.getParameter("adPwd");
        String pwd1=request.getParameter("adPwd1");
        int roleId=Integer.valueOf(request.getParameter("roleId"));
        System.out.println(name+"  "+pwd+"   "+pwd1+"   "+roleId);
        if(name==null||pwd==null||pwd1==null||roleId==0||"".equals(name)||"".equals(pwd)||"".equals(pwd1)){
            request.setAttribute("msg","请确保信息的完整性!");
            request.setAttribute("url","3;URL=getAdminAll");
            request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
        }else if(pwd.equals(pwd1)){
            boolean isOk=new AdminDao().updateAdmin(adId, roleId, name, pwd);
            if(isOk){
                request.setAttribute("msg","添加操作成功!");
                request.setAttribute("url","3;URL=getAdminAll");
                request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
            }else{
                request.setAttribute("msg","添加操作失败了!");
                request.setAttribute("url","3;URL=getAdminAll");
                request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
            }
        }else{
            request.setAttribute("msg","两次密码不一致!");
            request.setAttribute("url","3;URL=getAdminAll");
            request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
        }

    }

    /**
     * 添加
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void addAdmin(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String name=request.getParameter("adName");
        String pwd=request.getParameter("adPwd");
        String pwd1=request.getParameter("adPwd1");
        int roleId=Integer.valueOf(request.getParameter("roleId"));
        System.out.println(name+"  "+pwd+"   "+pwd1+"   "+roleId);
        if(name==null||pwd==null||pwd1==null||roleId==0||"".equals(name)||"".equals(pwd)||"".equals(pwd1)){
            request.setAttribute("msg","请确保信息的完整性!");
            request.setAttribute("url","3;URL=getAdminAll");
            request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
        }else if(pwd.equals(pwd1)){
            boolean isOk=new AdminDao().addAdmin(roleId, name, pwd);
            if(isOk){
                request.setAttribute("msg","添加操作成功!");
                request.setAttribute("url","3;URL=getAdminAll");
                request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
            }else{
                request.setAttribute("msg","添加操作失败了!");
                request.setAttribute("url","3;URL=getAdminAll");
                request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
            }
        }else{
            request.setAttribute("msg","两次密码不一致!");
            request.setAttribute("url","3;URL=getAdminAll");
            request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
        }
    }

    /**
     * 锁定
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void block(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        int id=Integer.valueOf(request.getParameter("id"));
        boolean isOk=new AdminDao().block(id);
        if(isOk){
            request.setAttribute("msg","锁定操作成功!");
            request.setAttribute("url","3;URL=getAdminAll");
            request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
        }else{
            request.setAttribute("msg","锁定操作失败了!");
            request.setAttribute("url","3;URL=getAdminAll");
            request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
        }

    }

    /**
     * 解锁
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void unblock(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        int id=Integer.valueOf(request.getParameter("id"));
        boolean isOk=new AdminDao().unblock(id);
        if(isOk){
            request.setAttribute("msg","解锁操作成功!");
            request.setAttribute("url","3;URL=getAdminAll");
            request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
        }else{
            request.setAttribute("msg","解锁操作失败了!");
            request.setAttribute("url","3;URL=getAdminAll");
            request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
        }

    }

    /**
     * 删除
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void delete(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        int id=Integer.valueOf(request.getParameter("id"));
        boolean isOk=new AdminDao().delete(id);
        if(isOk){
            request.setAttribute("msg","删除操作成功!");
            request.setAttribute("url","3;URL=getAdminAll");
            request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
        }else{
            request.setAttribute("msg","删除操作失败了!");
            request.setAttribute("url","3;URL=getAdminAll");
            request.getRequestDispatcher("Admin/msg.jsp").forward(request, response);
        }

    }

    /**
     * 获得管理员列表
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void getAdminAll(HttpServletRequest request,HttpServletResponse response) 
            throws ServletException, IOException {
        List<Map<String, Object>> lma =new AdminDao().getAdminAll();
        request.setAttribute("lma", lma);
        request.getRequestDispatcher("Admin/rolesList.jsp").forward(request, response);
    }

    /**
     * 注销
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void logout(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("athl_name");
        session.removeAttribute("athl_role");
        Cookie[] cookies =request.getCookies();
        if(cookies!=null){
            Cookie cook = new Cookie("myNR","");
            cook.setMaxAge(0);
            response.addCookie(cook);
        } 
        response.sendRedirect("Admin/login.jsp");

    }

    /**
     * 登录
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void login(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*request.setCharacterEncoding("utf-8");*/
        String name = request.getParameter("username");
        String pwd = request.getParameter("password");
        String code = request.getParameter("code");
        String mycookie = request.getParameter("mycookie");
        String ip = request.getRemoteAddr();
        if ("".equals(name) || "".equals(pwd) || "".equals(code)) {
            request.setAttribute("msg", "账户、密码、验证码不能为空!");
            request.getRequestDispatcher("./Admin/login.jsp").forward(request,
                    response);
        } else {
            int b = new AdminDao().login(name, pwd, ip);
            HttpSession session = request.getSession();
            String sessionCode = (String) session.getAttribute("safecode");
            if (b == 1 && sessionCode.equals(code)) {
                int role = new AdminDao().checkRole(name);
                session.setAttribute("athl_name", name);
                session.setAttribute("athl_role", role);
                if ("1".equals(mycookie)) {
                    Cookie cook = new Cookie("myNR", name + ":" + role);
                    cook.setMaxAge(60*60*24);
                    response.addCookie(cook);
                }
                response.sendRedirect("Admin/index.jsp");
            } else if (b == 0 && sessionCode.equals(code)) {
                request.setAttribute("msg", "该账户已被管理员锁定,请联系管理员!");
                request.getRequestDispatcher("./Admin/login.jsp").forward(
                        request, response);
            } else {
                request.setAttribute("msg", "账户、密码、验证码错误! 或  该账户不存在!");
                request.getRequestDispatcher("./Admin/login.jsp").forward(
                        request, response);
            }
        }
    }
}

谢谢支持!

资源下载:http://download.csdn.net/detail/jul_11th/9791947

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值