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);
        }

    }

  • 相关阅读:
    数据存储之iOS断点续传
    使用MVC4,Ninject,EF,Moq,构建一个真实的应用电子商务SportsStore(十一)
    使用MVC4,Ninject,EF,Moq,构建一个真实的应用电子商务SportsStore(十)
    使用MVC4,Ninject,EF,Moq,构建一个真实的应用电子商务SportsStore(九)
    获取当前页面url中的参数 coffeescript+node.js+angular
    自定义异步线程池工具,用于执行异步方法
    @ComponentScan 扫包 @Import添加组件
    properties解决中文乱码
    Spring Cloud Config配置中心(五)
    Spring Cloud Zuul路由转发(四)
  • 原文地址:https://www.cnblogs.com/siashan/p/3916615.html
Copyright © 2011-2022 走看看