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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:27:26+00:00 2026-05-16T08:27:26+00:00

I am sorry if my question does not make any sense. So here is

  • 0

I am sorry if my question does not make any sense. So here is what I have: 2 pages, A.jsf and B.jsf. When I press a button in A.jsf, the code set the value of an object and redirect to B.jsf. Contain of B.jsf will depend on which object I set in A.jsf (which depend on which button I click). Therefore, I dont want to allow the user to type this on the web browser

http://myhost:myport/myproject/B.jsf

and get directly to B.jsf. So, no GET request to B.jsf, only POST. And if I see GET request to B.jsf, I redirect to A.jsf. I feel like the solution is inside web.xml.
btw, I am using Netbean 6.8 and java EE 6

EDIT
Here is the solution. Thanks to BalusC
MyFilter.java

package org.xdrawings.filter;

public class MyFilter implements Filter{

    private FilterConfig filterConfig = null;

    public void destroy(){}

    public void init(FilterConfig filterConfig){
        this.filterConfig = filterConfig;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException{
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse res = (HttpServletResponse) response;
        if("GET".equals(req.getMethod()) && "/B.jsf".equals(req.getServletPath())){
            res.sendRedirect("A.jsf");
        }else {
            chain.doFilter(request, response);
        }
    }
}

then in my web.xml

<filter>
    <filter-name>My Filter</filter-name>
    <filter-class>org.xdrawings.filter.MyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>My Filter</filter-name>
    <url-pattern>*.jsf</url-pattern>
</filter-mapping>

All credits go to BalusC

  • 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-16T08:27:27+00:00Added an answer on May 16, 2026 at 8:27 am

    Yes, as you guessed, this can be controlled from in the web.xml. You need to declare a security-constraint on an url-pattern of /b.jsf with http-method of GET, along with an empty auth-constraint.

    <security-constraint>
        <display-name>Prevent GET requests on the b.jsf file</display-name>
        <web-resource-collection>
            <web-resource-name>The b.jsf file</web-resource-name>
            <url-pattern>/b.jsf</url-pattern>
            <http-method>GET</http-method>
        </web-resource-collection>
        <auth-constraint />
    </security-constraint>
    

    This however shows a HTTP 403 error. This doesn’t (and can’t) redirect to a.jsf (at least, not without supplying a custom 403 error page with the necessary checks, but that’s plain ugly). If the redirect is mandatory, other solutions are available:

    1. In the constructor of the request scoped bean associated with b.jsf check for postback by ResponseStateManager#isPostback() and then redirect accordingly using ExternalContext#redirect().

      FacesContext context = FacesContext.getCurrentInstance();
      if (!context.getRenderKit().getResponseStateManager().isPostback(context)) {
          context.getExternalContext().redirect("a.jsf");
      }
      

      If you’re already on JSF 2.0, you can also use the convenience method FacesContext#isPostback() instead, which is less typing.

    2. Or as a more high-level approach, create a Filter which does exactly the task. The requested page and the HTTP method can be obtained by HttpServletRequest#getServletPath() and getMethod() respectively and the redirect can be done using HttpServletResponse#sendRedirect().

      HttpServletRequest req = (HttpServletRequest) request;
      HttpServletResponse res = (HttpServletResponse) response;
      if ("GET".equals(req.getMethod()) && "/b.jsf".equals(req.getServletPath())) {
         res.sendRedirect("a.jsf");
      } else {
         chain.doFilter(request, response);
      }
      

      This can be more easily extended for future methods and pages which you can configure as under each init-param of the Filter in web.xml.

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

Sidebar

Related Questions

Sorry for the basic question - I'm a .NET developer and don't have much
Sorry for the second newbie question, I'm a developer not a sysadmin so this
Sorry for this not being a real question, but Sometime back i remember seeing
Sorry if this sounds like a really stupid question, but I need to make
Newbie question … sorry ;-) I have to write and to integrate a new
Sorry if the question is confused, as I'm confused myself. I'm working around these
Sorry for the long question title. I guess I'm on to a loser on
Sorry for the simple question but I feel like there's a smarter way to
Sorry for the slightly noobish question, as I am writing my first rails app.
Sorry in advance for the long question. What I'm really interested in is a

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.