Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to tell if a servlet forwarded the user to this JSP

 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want users to be able to directly access a particular JSP by typing in the URL to it. I mean, if they do that, I want the JSP to immediately forward them to the servlet that will eventually forward them to that JSP. What can I put at the top of the JSP to check and see if a servlet just forwarded them to this JSP, and if not, then forward (?) them on to the servlet? When a servlet forwards to a JSP, is it basically a GET request, or slightly different since it's all on the server side?

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why go through all that hassle?

Simply place the JSP somewhere in a folder structure under WEB-INF and there will be no way for it to be accessed directly via URL. I usually create a hierarchy rooted at WEB-INF/pages in which I place all my JSPs.

Placing them there does not prevent a servlet from forwarding to the page, but does prevent them from being directly accessed.
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree wholeheartedly seeing as how that is much cleaner design, but right now I just need a quick short-term hack if one is available. I don't want to both with extra request parameters or bean variables if that's what would be necessary. Is it possible to check the request and see if the request was for the JSP or the servlet? (I mean, check the request URL or something like that).

Even though it doesn't necessarily sound that way from here, I guess forward(request, response) essentially calls a GET on the resource you're forwarding to (or something that overwrites the needed info in the request), because at the top of my JSP, request.getRequestURI says the JSP no matter whether I access the JSP directly or first call a GET on the servlet that forwards me to the JSP.

Rats.
[ November 09, 2004: Message edited by: Stephen Huey ]
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaaaaaaaaaaaaaaaaah...getRequestURL() does the trick! Not getRequestURI()


 
ernest fakudze
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bear,

I like your idea of hiding your pages inside the WEB-INF folder. I think I will do that in my future apps. So thanks for sharing. Unless there are some caveats that I need to be aware of?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64721
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless there are some caveats that I need to be aware of?


Not really. It just means that all your JSPs must be accessed through servlet controllers (as is right and proper).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic