File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Problem using JSTL c:redirect. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Problem using JSTL c:redirect." Watch "Problem using JSTL c:redirect." New topic
Author

Problem using JSTL c:redirect.

Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Folks ,
I'm trying to use the jstl c:redirect tag for the first time , and facing a problem .The code snippet below is part of the header include in my web page . I intend to check whether a valid user exsists each time in the session when loading the page , else redirect to the login page.


However, I can't seem to figure out how to set the url . When I set it to /pages/login.jsp The container looks for it in ProjectName/pages/pages/login.jsp and when I set it to login .jsp it looks in ProjectName/login.jsp , I just Cant seem to get it to point at ProjectName/pages/login.jsp which is the required path .
Could anyone kindly help me out .
Thanks.
I've attached the snapshot of my working directory below.
EDIT: I also tried to replace the <c:redirect > tag with the <jsp:forward> , however that ended up with tomcat crashing throwing a StackOverFlow error. Wonder what could be going so wrong here


[Thumbnail for redirect issue.JPG]



Learning and Learning!-- Java all the way!
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Any other ideas , to persist the user credentials in the session are welcome too
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

Vic Hood wrote:However, I can't seem to figure out how to set the url . When I set it to /pages/login.jsp The container looks for it in ProjectName/pages/pages/login.jsp and when I set it to login .jsp it looks in ProjectName/login.jsp , I just Cant seem to get it to point at ProjectName/pages/login.jsp which is the required path .


Then the third thing to try would be "pages/login.jsp".
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

And by the way, you could do that redirect-if-not-logged-in check in a Filter, instead of including it in every single one of your JSPs.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Paul,
I tried that too.It seems to redirect to pages/pages/login.jsp again . And could you further elaborate on how to use filters for the same?
Thank you for your time!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

Filters: http://www.oracle.com/technetwork/java/filters-137243.html
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for that Paul! Will try implementing it ,but any idea why the c:redirect and jsp foward tags are failing?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18135
    
    8

Nope. It never occurred to me that I would ever need to redirect from a JSP. However you might try using <c:url> to specify your URL.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Redirecting from a JSP is huge red flag that you are doing it wrong. Amy redirecting or forwarding decisions should be made in conrollers (or filters) before the JSP is invoked.

Have you not read this article?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Thanks for posting Bear!
Well upon the lines of your suggestions , I've written a Filter class do the authentication job as below . This is the first time I'm writing one and would appreciate if anyone could point out what I'm doing wrong .

With the following mapping in web.xml


And the servlet responsible for the login page is as below.

The problems that I currently face are -
1)The CSS and images are missing when Im redirected to the login page through the filter , the URL for the login page however remains the same.
2)When I try to login the filter keeps throwing me onto the login page all the time . I did try some logging and found out that the value of the session attribute is being set to null . Any idea why this could be happening ?
Thanks .
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

1. Most likely you are using improper resource references in the JSP.

2. Why are you checking for the name in User rather than User itself, and why are you checking authentication int he filter and in the servlet?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Bear,
Thanks for the quick reply
1.This is the way that I use to reference my resources in the jsp , I've been trying to use something suggested a javaRanch article that youd earlier pointed out .
As below,

and the path displayed upon checking the source is as required.
2. I'm not sure whether you are hinting at doing the below in the filter?

3. The reason for checking in the filter and in the servlet are that , if I come in from another jsp page and not logged in I m redirected to the login page through the filter and in the case of entering wrong credentials in the login page Im redirected back to the login page through the servlet .
I don't know if that would be the right approach though.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

Try the CSS URL in the browser. Is it correct?

You are making this way too complicated. You should not have to worry about authentication anywhere but in the filter.
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Hi Bear ,
Thanks for replying !
1.
When I try to access the url of the css without the filter(tried after removing the mapping in the web.xml it works well). It just doesnt show up saying resource not found , but when I access the url through the jsp directly the css is applied and i can access the css through the browser
Below is the url generated in both cases.
/CISSource/css/CIS.css
But when I access the jsp using the filter and try to access the css the message i get is --File not found . However , without the filter I can acess my css file . I think the filter is restricting access to the css too.
2. Any pointers on how I should modify my servlet/filter to do the same ?
EDIT:The reason for doing the same , is that I was not aware of using filters when I wrote the page , and hence I had to send to the user back to the login page if the username/password combination was incorrect with a message .I'm not aware on how to go about this by using filters.Any insight on that would be helpful too.
Thanks
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

Fixed the first problem by changing the filter mapping to map *.jsp as even the css was getting blocked . However, I think the problem lies with filter as now I get a message from firefox saying
-The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
And the login page is not loaded.

Edit: I think the problem causing code is below , based on the console output it generates when the page goes into an infinite loop.

Console Output--
Test
null
Redirecting to login page as user is null
Test
null
Redirecting to login page as user is null
Test
null
Redirecting to login page as user is null
Test
null
Redirecting to login page as user is null
Test
null
Redirecting to login page as user is null
Test
null
Redirecting to login page as user is null
......
I thinkit gets into an infinite loop here .
Any idea why this could be happening?
Vic Hood
Ranch Hand

Joined: Jan 05, 2011
Posts: 477

EDIT-- Fixed it ! . The problem seems to have been with the mapping . Works fine for now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem using JSTL c:redirect.
 
Similar Threads
Redirct page in Tilse with JSTL
Directing login errors to form-login-page -- spurious login error message
Action Classes (Struts) with Eclipse
How to include servlet response in a JSP?
Problem with welcome-file-list