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.
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
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.
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
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.
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.
Originally posted by Jeff Storey:
In terms of best practices though, should I expose the controllers with a .html extension
In a properly structured Model 2 app, all of your requests should fits hit a servlet page controller that forwards to a hidden JSP.
Those are most likely using the antiquated Model 1 pattern.
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
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.
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.
For a discussion of how we got where we got as far as web app patterns go, this article might be helpful.
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)?