为什么会话在浏览器关闭后没有被清除?

时间:2017-06-28 16:18:07

标签: java jsp session servlets servlet-filters

我一直在输出如下:

Inside AuthenticationFilter..
Inside Session!

为什么会话在浏览器关闭后没有被清除?我甚至在index.jsp中保存了页面session=false,但仍然没有工作..请有人查看下面的代码并帮助我在浏览器窗口关闭后清除会话。

以下是代码:

的index.jsp

<%@ page session="false" %>
<%

/* ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/login");
rd.forward(request, response); */

response.sendRedirect("login");

%>

loginServlet

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
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 javax.servlet.http.HttpSession;

/**
 * Login servlet.
 */

@WebServlet("/login")
public final class LoginServlet extends HttpServlet {

    private HttpSession session = null;
    private RequestDispatcher rd = null;

    /**
     * Respond to a GET request for the content produced by
     * this servlet.
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are producing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    @Override
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
      throws IOException, ServletException {

        System.out.println("Inside Login Servlet...");

        PrintWriter out = response.getWriter();
        out.print("<h3> This is login servlet poage ... </h3>");

    }
} 

AuthenticationServlet

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebFilter("/*")
public class AuthenticationFilter implements Filter{

    private HttpSession session = null;

    public void init(FilterConfig arg0) throws ServletException {}  

    public void doFilter(ServletRequest req, ServletResponse resp,  
        FilterChain chain) throws IOException, ServletException {

        System.out.println("Inside AuthenticationFilter..");

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse)resp;

        session = request.getSession(false);
        if(session != null && !session.isNew()) {
            System.out.println("Inside Session!");
        } else {
            getAccessToken(request, response, chain);
            chain.doFilter(request, response);
        }

    }  

    public void destroy() {}  

    private void getAccessToken(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException {

        session = request.getSession();
        System.out.println("Created new  Session!");
    }

}

4 个答案:

答案 0 :(得分:0)

浏览器无法清除&#34;会话变量。他们没有直接访问$ _SESSION。他们可以清除会话ID cookie,给他们一个全新的空会话,或者你的服务器端代码必须清空$ _SESSION

答案 1 :(得分:0)

会话超时在web.xml中指定。即使您关闭浏览器,Session也将处于活动状态。 如果您真的需要结束它,请使用javascript onbeforeunload方法发出请求并调用

    public class Notification extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseNotification";

    @Override
    public void onMessageReceived(final RemoteMessage remoteMessage) {
        Map<String, String> data=null;

        Log.v(TAG,"notification Received");
        Log.v(TAG,remoteMessage.getData().toString());
    }
    }

答案 2 :(得分:0)

您无法检测浏览器何时关闭服务器端。这样做,您必须编写一些javascript来检测浏览器关闭,然后向您的服务器发出请求,使该用户的会话无效session.invalidate()

答案 3 :(得分:0)

此代码将在5分钟后使会话无效。

session.setMaxInactiveInterval(300); // 300 seconds

没有服务器端解决方案来检测浏览器事件。

如果您仍然需要检测浏览器关闭,请参阅this线程以获取客户端解决方案。