• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Servlet Mapping

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. I would not recommend using filters as page controllers. Servlets are better suited for that task.
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear et al, thanks for your help!
 
Jeff Storey
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic