res.getPathTranslated() does not return correct URL of the page requested
Hi,
The res.getPathTranslated() statement in the below code (in doFilter method) does not return the correct URL of the requested webpage.
Whenever a web page is accessed using a return statement (eg : return "nextPage"; ) inside a button's action method or a hyperlink's action method, the res.getPathTranslated() returns the URL of the current webpage instead of returning the URL of the webpage that is actually requested.
For example if there is a button on the page http://localhost:29080/MyJaas/faces/firstPage.jsp
And the button_action() is as follows
button_action()
{
return "nextPage";
}
The res.getPageTranslaged() returns "http://localhost:29080/MyJaas/faces/firstPage.jsp" instead of "http://localhost:29080/MyJaas/faces/nextPage.jsp"
However, if the webpage is requested by populating the URL property of the hyerlink in creator IDE, the res.getPathTranslated() returns the correct (requested) web page.
How to make res.getPathTranslagted() return the correct URL when the webpage is accessed from hyperlink's / button's action method?
I know that the explation is not very clear, so please bear with me. Let me know if you need more clarificatons. Thanks in advance for showing interest in this issue.
And by the way, the code below is the same as that used in Jaas Authentication tutorial :- http://developers.sun.com/prodtech/javatools/jscreator/reference/techart/2/jaas _authentication.html
package jaasauthentication;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
publicclass SecurityFilterimplements Filter{
/** Creates a new instance of SecurityFilter */
privatefinalstatic String FILTER_APPLIED ="_security_filter_applied";
public SecurityFilter(){
}
publicvoid init(FilterConfig filterConfig){
}
publicvoid doFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws java.io.IOException, ServletException{
HttpServletRequest req = (HttpServletRequest)request;
HttpServletResponse res = (HttpServletResponse)response;
HttpSession session = req.getSession();
String requestedPage = req.getPathTranslated();
String user=null;
//We dont want to filter certain pages which include the Login.jsp/Register.jsp/Help.jsp
if(request.getAttribute(FILTER_APPLIED) ==null){
//check if the page requested is the login page or register page
if((!requestedPage.endsWith("Login.jsp")) && (!requestedPage.endsWith("Register.jsp")) && (!requestedPage.endsWith("Help.jsp"))){
//Requested page is not login.jsp or register.jsp therefore check for user logged in..
//set the FILTER_APPLIED attribute to true
request.setAttribute(FILTER_APPLIED, Boolean.TRUE);
//Check that the session bean is not null and get the session bean property username.
if(((jaasauthentication.SessionBean1)session.getAttribute("SessionBean1"))!=null){
user = ((jaasauthentication.SessionBean1)session.getAttribute("SessionBean1")).getUsername();
}
if((user==null)||(user.equals(""))){
res.sendRedirect("Login.jsp");
return;
}
}
}
//deliver request to next filter
chain.doFilter(request, response);
}
publicvoid destroy(){
}
}

