如何使用 out extends HttpServlet 类生成新的 session ID。是否必须扩展 HttpServlet 类 & 是否必须在 doGet 方法中生成新的 session ID
public class LoginSupport extends ActionSupport {
public void prepare() {
HttpSession session = ServletActionContext.getRequest().getSession();
session.invalidate();
//How to genarate new session id
}
}
最佳答案
在调用HttpSession#invalidate()
之后,您可以通过调用HttpServletRequest#getSession()
来创建一个新 session 。
例如
public void prepare() {
final HttpServletRequest request = ServletActionContext.getRequest();
request.getSession().invalidate();
// generate new session (and id)
final HttpSession newSession = request.getSession();
}
来自您服务器的下一个 HTTP 响应应该包含一个新的 session ID,例如
Set-Cookie: JSESSIONID=6a303082951311647336934;path=/
来自 https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html#getSession--
getSession
HttpSession getSession()
Returns the current session associated with this request, or if the request does not have a session, creates one.
关于servlets - 生成新的 session ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48899358/