客户端禁用cookie情况下的URL重写

时间:2022-08-06 20:12:51

客户端禁用cookie情况下的URL重写:

servlet:

package com.stono.servlet.listenerorder;
import java.io.IOException;
import java.io.PrintWriter;
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 SessionON extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
// 如果客户端的cookie禁用,每次获得的session都是新的;在chrome Developer Tools中的Cookies界面可以看到没有request的JSESSIONID;
// 判断获得的session是否是新的;
boolean new1 = session.isNew();
System.out.println("session is " + new1);
PrintWriter writer = resp.getWriter();
writer.println("com.stono.servlet.listenerorder.SessionON.doGet(HttpServletRequest, HttpServletResponse)");
// 使用response.sendRedirect(response.encodeRedirectURL("b.do"))进行URL重写;
// 在客户端可以看到形如:TestBootWeb/b.do;jsessionid=6047B41B7A2D066E803FBA68C88E40F8的格式的URL;
// 这样在b.do中其session就可以记住当前的页面a.do的session;
resp.sendRedirect(resp.encodeRedirectURL("b.do"));
// 在JSP中使用response.encodeURL("xxx.do");进行编写;
}
}