cookie session 和登录验证

时间:2022-08-31 11:34:49

cookie、session 简单理解

cookie 和 session 的目的是追踪用户会话,标识用户,让服务器知道是谁在访问网站.

cookie 是保存在客户端的用户信息,在浏览器首次访问服务器时由服务器创建并发给客户端。浏览器收到cookie后会根据cookie时效,选择将cookie内容保存在内存或硬盘。当浏览器再次访问服务器是会在请求中携带cookie。

session 是保存在服务端的用户信息,服务器为每个会话创建一个session,session里面可以保存用户信息,session是通过以cookie的方式发送session ID 给客户端作为标识。来识别用户。而浏览器每次访问都会携带session ID

所以当cookie被禁用时也会导致session不可用。通常情况下,服务器是通过session保存用户登录信息,验证用户是否登录。那么当cookie不可用时,如何验证用户登录?
一般有下面几种方法:
1 URL重写
response对象有一个encodeURL(String URL)方法.可以自动的给url添加session ID 后缀,以提交参数的方式携带session ID
例如:

String newURL = response.encodeURL("/cart/ProductList");
print(newURL) -> /cart/ProductList;jsessionid=110E31172D5630A31E296D40730B3521

这样,服务器发给浏览器的page里的link都如此处理。那服务器也能确认用户。

2 form 表单隐式提交
3 http协议携带

用户登录实现

用户登录逻辑:用户从登录页面提交登录信息,验证通过就跳转到welcome页面,不通过就回到登录页面,并提示信息错误。 使用sision保存用户登录信息,如果cookie被禁用,就使用url重写技术
login.jsp

<%@ 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>login</title>
</head>
<body>
<h1>登录</h1>
<form action="login" method="post">
    name: <input type="text" name="name">
    password: <input type="password" name="password">
    <input type="submit" value="login">     
    <input type="button" value = "注册" onclick="/cart/register">
</form> 
<!--提示登录错误信息-->
<p>${warning}</p>

</body>
</html>

LoginServlet.java

package servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import DAO.UserDAO;
import bean.User;

/** * Servlet implementation class Login */
@WebServlet("/Login")
public class LoginServlet extends HttpServlet {

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

        //获取客户端传递的参数
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        //查数据库
        User user = new UserDAO().getUser(name,password);

        if(user == null){
            request.setAttribute("warning","user name or password is wrong");
                   request.getRequestDispatcher("/login.jsp").forward(request,response);
        }else{

            //session要占用服务器内存,所以服务器不会默认创建(没有设置的话jsp会创建一个session对象(jsp隐式对象之一))
            //没有创建session的话 encodeURL()不会起效
            request.getSession().setAttribute("username",name);

            String URL = response.encodeURL("/cart/ProductList"); 
            request.setAttribute("URL",URL);

            request.getRequestDispatcher(("/welcome.jsp")).forward(request,response);
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

welcome.jsp

<!--其他省略-->
<body>
WELCOME ${username}
<br/>
<a href="${URL}">product list</a>
<P>${URL}</P>
</body>