首页 > 代码库 > 09-利用session完成用户登陆

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="http://www.mamicode.com/登录">;
    </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="http://www.mamicode.com/day07/login.html">登录</a><a href="http://www.mamicode.com/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);
    }

}