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 8135643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:29:05+00:00 2026-06-06T10:29:05+00:00

below is my code; /* * To change this template, choose Tools | Templates

  • 0

below is my code;

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package racms;

import java.io.IOException;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


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

    @Override
    public void init(FilterConfig config) throws ServletException {
        // If you have any <init-param> in web.xml, then you could get them
        // here by config.getInitParameter("name") and assign it as field.
    }

    @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);

        String pageRequested = request.getRequestURI().toString(); 
        //try{
        //FacesContext fctx = FacesContext.getCurrentInstance();
        //NavigationHandler myNav = fctx.getApplication().getNavigationHandler();



         if(session==null){       
               session = request.getSession(true); // will create a new session     
               response.sendRedirect("Login.xhtml");       
               //myNav.handleNavigation(fctx, null, "Login");
         }else if(session==null && pageRequested.contains("Login.xhtml")){       
                //  session.getAttribute("user");     
                chain.doFilter(request, response); // continue filtering       
         }else if((session.getAttribute("user")== null) && (!pageRequested.contains("Login.xhtml"))){          
             response.sendRedirect("Login.xhtml");
             //myNav.handleNavigation(fctx, null, "Login");
         }else {
             chain.doFilter(request, response);
         }
        //}catch(Exception e){
        //    System.out.println("Error :"+ e);
        //}
        /*if ((((HttpServletRequest) req).getSession().getAttribute("user") == null)) {
            FacesContext fctx = FacesContext.getCurrentInstance();
            NavigationHandler myNav = fctx.getApplication().getNavigationHandler();
            myNav.handleNavigation(fctx, null, "Login");
            //response.sendRedirect(request.getContextPath() + "/Login.xhtml"); // No logged-in user found, so redirect to login page.
        } else {
            chain.doFilter(req, res); // Logged-in user found, so just continue request.
        }*/
    }

    @Override
    public void destroy() {
        // If you have assigned any expensive resources as field of
        // this Filter class, then you could clean/close them here.
    }

}

If i use FacesContext.getCurrentInstance(), java.lang.Nullpointer Exception occurs;
If i use response.sendRedirect(“Login.xhtml”); it shows a blank page, if i do view source, i can see the source of Login.xhtml in jsf. it is not rendered to html..

what i want to do is: if the user is not logged in and accessing any page then send him to Login.xhtml, if the user is on Login.xhtml then show him the page to login.

please help..

  • 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-06T10:29:06+00:00Added an answer on June 6, 2026 at 10:29 am

    Redirect to an URL that matches the FacesServlet mapping. Apparently it is mapped on /faces/* instead of *.xhtml. Then redirect to faces/Login.xhtml.

    Here’s a rewrite, which simplifies the logic:

    @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);
        User user = (session != null) ? (User) session.getAttribute("user") : null;
        String loginURL = request.getContextPath() + "/faces/Login.xhtml"; 
    
        if (user == null && !request.getRequestURI().equals(loginURL)) {       
            response.sendRedirect(loginURL);
        } else {
            chain.doFilter(request, response);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use below code for change keyboard layout from code but occur below warning
i am using below code to change the the font type of text view.
I am using the code below to change to shape of the winform. It's
I am trying to change desktop image using below code: NSError *anError = nil;
Issue In the below code , say if i want to change the color
When I run the below code, everything goes great until I change the value
I am using the below JS code in order to change the class when
How to change the .page width value below, programmatically in the code behind page?
Similar to this post IEnumerable model property in an ASP.NET MVC 3 Editor Template
I'm writing code this week to dynamically change the color of images. As long

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.