• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem using JSTL c:redirect.

 
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
redirect-issue.JPG
[Thumbnail for redirect-issue.JPG]
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any other ideas , to persist the user credentials in the session are welcome too
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Filters: http://www.oracle.com/technetwork/java/filters-137243.html
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for that Paul! Will try implementing it ,but any idea why the c:redirect and jsp foward tags are failing?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
EDIT-- Fixed it ! . The problem seems to have been with the mapping . Works fine for now.
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic