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

  • SEARCH
  • Home
  • 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 9112695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:47:47+00:00 2026-06-17T03:47:47+00:00

How to redirect user to home page if user is already logged. I use

  • 0

How to redirect user to home page if user is already logged. I use Filter class for login page but it doesn’t work properly. my code is:

@WebFilter(filterName = "loginFilter",
urlPatterns ={"/login.xhtml"})
public class LoginFilter implements Filter{

    private FilterConfig filterconfig;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterconfig = filterconfig;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest httprequest =(HttpServletRequest) request;
        HttpServletResponse httpresponse =(HttpServletResponse) response;
        try{
        if(httprequest.getUserPrincipal() != null){

            System.out.printf("User authenticated with " + httprequest.getRemoteUser() + " username conected.");
            httprequest.getRequestDispatcher("/home.xhtml").forward(request, response);
        } else{
            chain.doFilter(request, response);
        }
        }catch(Exception){
            //do something
        }

    }

    @Override
    public void destroy() {
        System.out.print("Existing from loginFilter");
    }

}

Here is my problem: user logged to web and do some navigation and things and back to login page using browser’s back button without logout and again enter username password then press login button. Then it throws exception
IndexOutofBoundsException. I just need to check when user navigate to login page using either link or browser’s back button, and redirect to home page. any suggestion?

  • 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-06-17T03:47:48+00:00Added an answer on June 17, 2026 at 3:47 am

    Just tell the browser to not cache the login page. Add the following lines to the filter directly after you’ve casted the response to httpresponse.

    httpresponse.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
    httpresponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
    httpresponse.setDateHeader("Expires", 0); // Proxies.
    

    Otherwise the browser would give the page from cache on back button instead of sending a fullworthy request of the server which should trigger the filter.

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

Sidebar

Related Questions

In our application we use jsf,we have to redirect the user to home page
When I want to redirect a user to a specific page behind a login
How can I redirect a user to home page? Example: mywebsite.example/ddfdf/fdfdsf and I want
I want to redirect the user to login page on landing, While user is
I need to redirect user to another page called free.php from index page, if
I want to redirect the user to next page afte successfully submitting their information
I need to redirect the user to page not found page if the url
I need to redirect a user to a different page if they visit a
On session timeout we re-direct to the login page and if the user logs
I'm having 2 login forms in my (cake) application. One on the home page

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.