zoukankan      html  css  js  c++  java
  • 09-利用session完成用户登陆

    /***********************************************login.html*****************************************/

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="/day07/LoginServlet" method="post">
            用户名:<input type="text" name="username"><br>
            密码:<input type="password" name="password"><br>
            <input type="submit" value="登录">;
        </form>
    </body>
    </html>

    /*************************************LoginServlet***********************************************/

    package session;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;

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

    public class LoginServlet extends HttpServlet {
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = response.getWriter();
            String name = request.getParameter("username");
            String pwd = request.getParameter("password");
            
            List<User> list = Db.getAll();
            for(User user:list){
                if(user.getName().equals(name) && user.getPassword().equals(pwd)){
                    //登录成功就是向session存一个登录标记
                    request.getSession().setAttribute("user", user);//值是user对象
                    //跳到首页
                    response.sendRedirect(request.getContextPath()+"/index.jsp");
                    return;
                }
            }
            
            
            out.print("用户名密码不正确");
            
        }

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

    }
    class Db{
        private static List list = new ArrayList();
        static{
            list.add(new User("aaa","123"));
            list.add(new User("bbb","123"));
            list.add(new User("ccc","123"));
        }
        public static List getAll(){
            return list;
        }
    }
    /*****************************************************************index.jsp*******************************************************************8/

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        welcome:${user.name}<a href="/day07/login.html">登录</a><a href="/day07/LogoutServlet">退出登录</a>
    </body>
    </html>

    /***********************************************LogoutServlet*************************************************/

    package session;

    import java.io.IOException;
    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 LogoutServlet extends HttpServlet {
        
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            HttpSession session = request.getSession(false);
            //先判断session是否为空,空就代表没有用户登录,这是就不做处理,继续跳回到首页
            if(session == null){
                response.sendRedirect("/day07/index.jsp");
                return;
            }
            //不为空就摧毁session跳回到首页
            session.removeAttribute("user");
            response.sendRedirect("/day07/index.jsp");
        }

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

    }

  • 相关阅读:
    jQuery插件:用于获取元素自身的HTML内容
    自定义 Web 部件用户界面简介
    在MOSS2010中实现OU下的用户的上下级组织关系
    sharepoint2010人性化的地方--员工离职AD账号禁用(个人网站自动提醒上级经理功能)
    SharePoint2010文档归档策略(2)-从放置库转移到自己定义的文档库
    SharePoint2010文档归档策略
    如何用VS2010在SharePoint中创建自定义字段类型(以eWebEditor为例)
    如何实现SP文档库类似百度文档库的效果 (副标题:如何在SP2013文档库的SWF文件用FlexPager显示)
    查看SharePoint文档库是,显示层次目录,可以点击返回层次
    安装和配置SharePoint 2013 with SP1 Workflow
  • 原文地址:https://www.cnblogs.com/siashan/p/3916615.html
Copyright © 2011-2022 走看看