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
Yes, as you guessed, this can be controlled from in the
web.xml. You need to declare asecurity-constrainton anurl-patternof/b.jsfwithhttp-methodofGET, along with an emptyauth-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:In the constructor of the request scoped bean associated with
b.jsfcheck for postback byResponseStateManager#isPostback()and then redirect accordingly usingExternalContext#redirect().If you’re already on JSF 2.0, you can also use the convenience method
FacesContext#isPostback()instead, which is less typing.Or as a more high-level approach, create a
Filterwhich does exactly the task. The requested page and the HTTP method can be obtained byHttpServletRequest#getServletPath()andgetMethod()respectively and the redirect can be done usingHttpServletResponse#sendRedirect().This can be more easily extended for future methods and pages which you can configure as under each
init-paramof theFilterinweb.xml.