[英]Unable to Close my Session Servlets
我無法在我的注銷方法中使用session.invalidate()關閉我的會話請幫忙!
public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
response.getWriter().println("<h3><a href='/assign2'>Logged out Successfully</a></h3>");
HttpSession session = request.getSession(false);
if(session!=null)
session.invalidate();
}
用戶名根本不會寫為null
這是我歡迎的頁面,我將重定向到哪里
HttpSession session=request.getSession(false);
if(session!=null)
{
if((request.getSession().getServletContext().getAttribute("userid")) != null)
{
username = request.getSession().getServletContext().getAttribute("userid").toString();
}
}
System.out.println(username);
注銷頁面沒問題,但在歡迎頁面中你混合了概念:
盡管session.invalidate
的執行解除了綁定所有綁定屬性,但您正在從ServletContext而不是Session中檢索屬性userid
。 此外,請注意,如果需要, request.getSession()
會創建一個新會話。
存儲和檢索屬性的一致方法是通過HttpSession對象:
HttpSession session=request.getSession(false);
if(session!=null)
{
if((session.getAttribute("userid")) != null)
{
username = session.getAttribute("userid").toString();
}
}
System.out.println(username);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.