Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using a conditional forward

 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone
I've got a set of JSPs where I want to be able to test on each one to see if there is a valid user logged in before displaying the page. If there is no user logged in then it should send the user to a message/login page.
Upon succesful login I set a session attribute with the users name and on each page I am testing for it like this:

In the generated code the forward action is being treated like it is regular html and being written to out.
I also tried to use a RequestDispatcher but get a NullPointerException because there is no Request object if the user just types in the url for the page.
All of the JSPs go to a controller servlet so I could just check there for a valid user but I really don't even want them to see the JSPs if they aren't logged in.
Anyone have any ideas how to make this work?
Thanks
Dave
 
Simon Brown
sharp shooter, and author
Ranch Hand
Posts: 1913
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you thought about using the declarative authentication that's now part of the servlet spec? If you don't want to use this, you could instead write a tag that performs your conditional logic for you and include it in a standard header.
Cheers
Simon
[ May 02, 2002: Message edited by: Simon Brown ]
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I had this exact problem, but it turns out it was for jsp:include

http://www.coderanch.com/t/350434/Servlets/java/Conditional-includes

I'm surprised the jsp:forward isn't working.

As for 'hiding' a JSP from a nosy user... I think the only way to do this is to protect them with the J2EE declarative security. Otherwise, you could perhaps surround the entire contents of the jsp with your security code.. if they're not logged in, the JSP will produce an empty document.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike and Simon
Thanks for the tips. Simon, I can't use anything too fancy it is actually for a school assignmnet and I'm adding some 'extras'.
In looking over the code in the topic Mike linked to though I figured it out - it's always the little things!!!- I had a space between my : and forward.
I hate that!!!
It all works fine now - thanks guys
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haha! I saw that, but I thought you had done it deliberately because UBB had munched it.
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike, never assume I know what I'm doing
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!