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 Servlet Mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Servlet Mapping" Watch "Servlet Mapping" New topic
Author

Servlet Mapping

Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Hi, I'm somewhat new to jsp and servlets (not to java, but to how all the wiring between the jsp and servlets work), so this is probably a basic question, but I'm not really sure what to do here.

I have a main page, index.jsp, that a user sees when they enter my site. I'd like this page to map to a servlet and display some dynamic information (which it stores in a session parameter). I've set up my servlet mapping and it works fine, but the problem is if I reforward the request back to index.jsp from the servlet, I end up in an in infinite loop since the forwarding causes another request to index.jsp, etc.

What I could do is create an index.html which is the welcome page and that gets mapped to the servlet and then forwards the request to index.jsp, and this works fine. The problem is if the user types in index.jsp directly, the session variable does not get set first since it is index.html that maps to the servlet.

Any help here would be greatly appreciated. Thanks.

Jeff


Jeff Storey
Software Developer
[url]http://jeffastorey.blogspot.com[/url]
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

The problem is if the user types in index.jsp directly, the session variable does not get set first since it is index.html that maps to the servlet.


In that case you can simply redirect him to the welcome page(index.html in this case), if the session or forwarded request is not present in the index.jsp.

Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Thanks, that is helpful. I do have a followup question though. Now say I have another page, other.jsp. I want to link to that page from index.jsp. But other.jsp also has dynamic content. Does that mean I need to link to some other.html page and map other.html to the servlet and then forward the request to other.jsp to avoid the same infinite loop? It seems like I would need an empty html page to correspond with each jsp page. How else could I access a page, have it map to a servlet, and then redirect back to the same page without a loop?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

If you are worried about users typing in index.jsp directly, remove the file. Then explore the welcome file setting of the deployment descriptor to set the welcome "file" to your servlet.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Bear,

You had mentioned the welcome file in a previous post (and that advice was helpful, thanks), but this issue this time was a little bit different. I had a page, let's call it X.jsp and it required a request variable to be set. So, what I was trying to do is, the first time X.jsp was accessed set the variable, get the dynamic data, and then load the page. This was causing an infinite loop since on each reload, it would again hit the servlet again, and so on. I realized the variable was more appropriately a servlet variable that did not change and could be looked up rather than being provided to each session and fixed my problem. I appreciate your help.

Jeff
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

If your JSP relies upon a scoped variable to be set, preferably by a page controller, hide the JSP behind WEB-INF. That way, it can never be addressed and can only be accessed via a forward from the page controller.
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

It seems like I would need an empty html page to correspond with each jsp page. How else could I access a page, have it map to a servlet, and then redirect back to the same page without a loop?


What will the empty html page do?
If its really empty then directly calling the servlet will also do even the other.jsp will also do.
To avoid the loop, you can use the response object forwarded by the servlet.
Maybe I am not getting your problem entirely but I think your approach to the problem is not among the best ones.


Hope this helps
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

To avoid the loop in the first place, follow best practices as I have outlined, and only allow addressing of the servlet controllers.
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Bear, I quickly learned what you are saying (this is the first time I've done jsp work, although I have many years of Java experience and other web languages, so hopefully this shouldn't be too difficult to pickup). In terms of best practices though, should I expose the controllers with a .html extension, or some other (say .do) extension, and then map that extension to a servlet? I was browsing some major sites that use jsp (such as http://www.mlb.com) and I've found that all of their page extensions end in .jsp and I know they are looking up dynamic content, so I'd assume they're running into the same type of looping issue and I was just trying to guess how they get around it. Thanks.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4174
    
  21

There are a handful of ways to do what you want. I think the best is to use Filters. See The Sun Article on the subject.

Your filter can be mapped to your JSP file of interest (or to all JSPs, or to all URLs...) and check for the presence of the scoped object. If it is present, then it simply continues with the filter chain, doing nothing. Otherwise it can forward the request to your servlet which does the business needed to set the variable.

Filters can be mapped to be run on forwards and include, or not, so you can easily avoid infinite loops.

All the while the JSPs can be directly navigated to by the client, and your Servlet URLs are hidden.


Steve
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

Originally posted by Jeff Storey:
In terms of best practices though, should I expose the controllers with a .html extension
No! That would create all manner of caching problems. You don't need an extension at all. What's all the hangup on extensions? Read the documentation for FrontMan (see sig), my Front Controller implementation.

In a properly structured Model 2 app, all of your requests should fits hit a servlet page controller that forwards to a hidden JSP.

I was browsing some major sites that use jsp (such as http://www.mlb.com) and I've found that all of their page extensions end in .jsp
Those are most likely using the antiquated Model 1 pattern.

so I'd assume they're running into the same type of looping issue and I was just trying to guess how they get around it.
No, they're not. The issue you are up against is pretty much unique to you because you are creating the problem by trying to loop around. Don't.

For a discussion of how we got where we got as far as web app patterns go, this article might be helpful.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

P.S. I would not recommend using filters as page controllers. Servlets are better suited for that task.
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
I've got it now. The model 1 pattern seems to do a bit of mixing of the view and controller so there is some logic (at least the calling of the business logic beans) from the jsp, a no-no in the MVC architecture. My loop issue got caused because I was trying to allow my users to access the jsp page directly, which I can now see is wrong for a variety of reasons (in addition, no reason to expose that I'm using JSP). I've found that I can simply hide the jsp page from the user so they'll never know it's there anyway. Or, even if I don't hide it, I can just redirect it to the front controller.

Just one last question though. Say I did allow users to hit the JSP pages directly (by typing www.mysite.com/mypage.jsp or something) and that page did require a variable to be set, I'd like to redirect to the front controller and then redirect the user back to the correct page. To do that, I've been using a naming convention such that mypage.do is mapped to a mypage servlet that displays it's content on mypage.jsp (so even when the user types mypage.jsp, they won't realize they're being directed to the controller). My question deals with my strategy here -- naming the .do page the same as the .jsp page. This way if the user types mypage.jsp, I know that should go through the mypage servlet since they are named the same. Is there a better way (aside from hiding the jsp all together)?

Thanks,
Jeff
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

To be honest I've never given that any thought and can't see why you would. Either you allow direct access to the JSPs or you don't. Anything in between is just a mess.
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Bear et al, thanks for your help!
Jeff Storey
Ranch Hand

Joined: Apr 07, 2007
Posts: 230
Bear,

One last question on this. I have a couple of static html pages in my site. Is it common to have some pages that have jsp extensions and some that have html (although I could use a mod rewrite to hide the extensions completely)?

Thanks,
Jeff
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60992
    
  65

Why not? If a page is completely static, it could just be an html page. Though, it's rare in my apps for there to be a completely static page.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Mapping