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 how to tell if a servlet forwarded the user to this JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "how to tell if a servlet forwarded the user to this JSP" Watch "how to tell if a servlet forwarded the user to this JSP" New topic
Author

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

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
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

Joined: Jul 15, 2003
Posts: 618
Aaaaaaaaaaaaaaaaaaaah...getRequestURL() does the trick! Not getRequestURI()


ernest fakudze
Ranch Hand

Joined: Aug 27, 2001
Posts: 216
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?


In a time of drastic change it is the learners who inherit the future. The learned usually find themselves equipped to live in a world that no longer exists.<br />Eric Hoffer
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to tell if a servlet forwarded the user to this JSP