Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6557023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:01:18+00:00 2026-05-25T13:01:18+00:00

I have a session validation Filter which logs off the user when session is

  • 0

I have a session validation Filter which logs off the user when session is expired.

Here is a piece of code but this is not working. Not working means this is not redirecting to the login page even if the session expires.

Please help me to resolve this issue.

public void doFilter(ServletRequest request, ServletResponse response, 
        FilterChain chain) throws IOException, ServletException {  
    HttpServletResponse res = (HttpServletResponse) response;  
    HttpServletRequest req = (HttpServletRequest) request;  

    HttpSession s = req.getSession(false);  

    if (s==null)
    {
        //redirect to login page with session expiry message   
    } else {  
        chain.doFilter(request, response);  
    }  
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T13:01:19+00:00Added an answer on May 25, 2026 at 1:01 pm

    I have a session validation Filter which logs off the user when session is expired.

    This makes honestly no utter sense. If you store the logged-in user as an attribute of the session and intercept the “logged-in” status based on the presence of the logged-in user in the session, then you do not need to manually logout the user at all when the session is expired. When the session expires, all its attribtues will get lost anyway and hence the user will be “automagically” logged out.

    Here’s an example of how you can login the user in the doPost() of a servlet which is invoked by a POST submit of the login form JSP.

    String username = request.getParameter("username");
    String password = request.getParameter("password");
    User user = userService.find(username, password);
    
    if (user != null) {
        request.getSession().setAttribute("user", user); // Login user.
        response.sendRedirect("userhome"); // Redirect to user home page.
    } else {
        request.setAttribute("errormessage", "Unknown login, try again"); // Set error message.
        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Redisplay login form.
    }
    

    You see, when the login is valid, the user is stored as a session attribute. The remnant of your code could just check if it is null or not to determine if the user is logged in. Whenever the session expires, it automatically becomes null.


    this is not redirecting to the login page , even if the session expires

    I have no idea what you’re trying to do since the initial functional requirement makes no sense. However, there exist two common functional requirements related to session expiration and the login page. I guess that you actually need either one of them:

    1. “How do I redirect the visitor to the login page when he requests a page which is restricted to logged-in users?”

      You need to create a filter and map it on the (common) URL pattern of the restricted page(s). In the filter, just check if the user is present in session and then continue the chain, else redirect to login page.

      @Override
      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
          HttpServletRequest request = (HttpServletRequest) req;
          HttpServletResponse response = (HttpServletResponse) res;
          HttpSession session = request.getSession(false);
      
          if (session == null || session.getAttribute("user") == null) {
              response.sendRedirect("login"); // No logged-in user found, so redirect to login page.
          } else {
              chain.doFilter(req, res); // Logged-in user found, so just continue request.
          }
      }
      

    2. “How do I automatically redirect the currently opened page to the login page when the session expires?”

      Use the <meta> refresh in combination with HttpSession#getMaxInactiveInterval().

      <meta http-equiv="refresh" content="${pageContext.session.maxInactiveInterval};url=sessionexpired.jsp">
      

      This will automatically redirect the current page to the given url whenever the session expires. The ${pageContext.session.maxInactiveInterval} expression will inline the session expiration time in seconds, which is exactly what the content attribute needs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Weired problem! ASP.NET Session expires instantly. In my web.config I have this session settings:
I have an NHibernate session. In this session, I am performing exactly 1 operation,
I have a table (session) in a database which has almost 72,000 rows. I
I have a /payments interface where the user should be able to filter via
Some people have told me that the following code is bad for HTML validation:
i have somthing like this: (if this page needs the user to be logged)
This is an issue which I have seen all across the web. I will
I have used regular expression in email field validation . But after i used
I have a big validation JS script. I want to put this script to
I have some problem with using jstl. I have this: <jsp:useBean id=view class=user.View></jsp:useBean> <jsp:useBean

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.