Servlet和jsp中跨应用的Session怎么取?
跨应用的Session
注意跨应用的Session是取不出来的。我的应用是ServletHello,第二个应用名字叫CookieTest,你可以观察到QueryCookie这个Servlet已经运行了,但就是Session取不出来。可以考虑用文件、数据库,URL传值、隐藏表单传递session id等。初学者不必关心。
package com;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class ServletHello2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession hs=request.getSession();
hs.setAttribute("isLogin", "true");
response.sendRedirect("http://localhost:8080/CookieTest/QueryCookie");
}
}
package com;
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 QueryCookie extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
HttpSession hs=request.getSession();
System.out.println("isLogin in Cookie Test Project is "+ hs.getAttribute("isLogin"));
}
}
更多内容请见原文,原文转载自:https://blog.csdn.net/qq_44638460/article/details/104231892