File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to make MVC controller servlet the Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to make MVC controller servlet the "default page"" Watch "How to make MVC controller servlet the "default page"" New topic
Author

How to make MVC controller servlet the "default page"

Philip Shanks
Ranch Hand

Joined: Oct 15, 2002
Posts: 189
I have an application that is heavily based on David Geary's MVC example in "Advanced JavaServer Pages".
In my entry page I need a JavaBean populated with some data, and so I used some code that looks like this:

And this works as it should; the script inside the <jsp:useBean> tag runs only if the bean doesn't already exist. The problem is that it is poor design to be forwarding (or doing any application logic at all) in a JSP view component.
Now, this code snippet is in index.jsp, which is essentially a "home page" of sorts for the application. As such, when a user goes to "http://myserver/myApp" they will enter correctly into the application. They return to this same page to pick different reports.
My question is this: I want to move that initial redirect code into the controller servlet where I feel that it belongs, as something like:

This assumes that the visitor now goes to the controller servlet first, and not index.jsp -- they will be directed to that as a view component after their session is properly initialized.
I can remove from the <welcome-files> list in web.xml, but how do I specify that the controller servlet is the default page UNLESS the session is properly init-ed?
The desired flow is
  • request the controller servlet to ensure session init
  • forward to index.jsp after session init -- this is done by "/StartAction.do"
  • pick a report or other application action
  • return to index.jsp to choose other reports -- this is currently accomplished by internal navigation

  • Have I sufficiently confused the issue?
    [ May 28, 2003: Message edited by: Philip Shanks ]

    Philip Shanks, SCJP - Castro Valley, CA
    My boss never outsources or has lay-offs, and He's always hiring. I work for Jesus! Prepare your resume!
    Asher Tarnopolski
    Ranch Hand

    Joined: Jul 28, 2001
    Posts: 260
    hmmm, sorry, i didn't really get what your problem is. ... just in case i did ...

    your sollution is right, your servlet checks the session for needed attribute and either continues with its methods or redirects the client to another servlet.
    to secure your servlet you can also play with the whole security-constraint stuff and to "open" your servlet for direct access to a group of clients ...
    once again, you can also try to explain your question once more


    Asher Tarnopolski
    SCJP,SCWCD
    Philip Shanks
    Ranch Hand

    Joined: Oct 15, 2002
    Posts: 189
    The crux of the problem is how to make the visitor start with the controller servlet instead of a welcome-page named in the web.xml file.
    Can I put the uri of the controller servlet in the <welcome-files> section of web.xml?
    Sam Walker
    Ranch Hand

    Joined: Nov 06, 2002
    Posts: 65
    Hi Philip,
    I think what you�re describing is changing your code from a model 1 to model 2 MVC -- Please someone correct me about this if I�m wrong. If I understand you correctly you want all your calls to be forwarded to a controller servlet that will then redirect them to the proper page, probably the url that's the target of the action statement of your html form.
    If I am correct then please read on because I did the same thing a while ago. I removed all forward and redirect statements from my JSP code.
    I removed all business code such as validation from JSP pages and put them in java classes as methods that were invoked from within the controller servlet.
    Finally I modified my web.xml defined my controller servlet and did servlet mapping. The third part is very dependent on the application server that you�re using and you could find a lot of information regarding the syntax in the documentary of the application server you�re using.
    I would appreciate any corrections or enhancements of what I've said. I hope this is what you're looking for.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60992
        
      65

    Sledge-hammer approach: use a meta tag in your index.html to immediately redirect to whatever servlet URL you want.
    hth,
    bear


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    David Hibbs
    Ranch Hand

    Joined: Dec 19, 2002
    Posts: 374
    Originally posted by Bear Bibeault:
    Sledge-hammer approach: use a meta tag in your index.html to immediately redirect to whatever servlet URL you want.
    hth,
    bear

    And just in case the META tag doesn't do it, also include some JavaScript...
    <SCRIPT Language="JavaScript">
    top.location.replace('/App/welcome');
    </SCRIPT>
    And make both an index.html and index.jsp, just for good measure.
    [ May 30, 2003: Message edited by: David Hibbs ]

    "Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
    Frank Carver
    Sheriff

    Joined: Jan 07, 1999
    Posts: 6920
    Note that version 2.4 of the servlet API addresses this well-known problem, and allows any servlet to be configured as a "welcome page".
    Of course there aren't many servlet containers which support version 2.4 yet


    Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
    Philip Shanks
    Ranch Hand

    Joined: Oct 15, 2002
    Posts: 189
    Sam: I am already using the Model 2 design. My only issue is for the very first request of the client's session.
    Bear & David: I had considered the redirect aproach, and have used this solution elsewhere. I was hoping, however, to find something a bit more elegant than a round trip between client and server. (BTW, for those cases where I don't know anything at all about the client, I usually use a combination of META-tag refresh and Javascript redirect triggered in the BODY onload().
    Frank: I think the change in the servlet 2.4 API addresses exactly my issue.
    For now, I think I will continue with my current solution. It isn't really a big deal, and it seems that the JSP API considers exactly this situation when it allows run-once code to occupy the body of a <jsp:useBean> tag.
    Thank you all for your input!
    [ May 31, 2003: Message edited by: Philip Shanks ]
    Sam Walker
    Ranch Hand

    Joined: Nov 06, 2002
    Posts: 65
    Thanks Philip,
    I hadn't understood your question.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60992
        
      65

    ...bit more elegant than a round trip between client and server

    Understood. If you make the page index.jsp, you can avoid a redirect by using a forward. (Rubber mallet approach?)
    hth,
    bear
    David Hibbs
    Ranch Hand

    Joined: Dec 19, 2002
    Posts: 374
    Originally posted by Bear Bibeault:

    Understood. If you make the page index.jsp, you can avoid a redirect by using a forward. (Rubber mallet approach?)

    That's a pretty slick idea! (Why didn't I thinnk of that?!?!)
    Re: "Rubber mallet approach" -- LOL!
    Philip Shanks
    Ranch Hand

    Joined: Oct 15, 2002
    Posts: 189
    Actually, the forward() approach is what I was using from the start.

    In this case the forward is contingent on the non-existence of the required JavaBean. I don't want the forward to happen, except on the very first call.
    After further research, I think that this could also have been accomplished with a Filter class, but with considerably more work.
    Also, I have read Filters are not supposed to contain application-specific code, so this approach would probably be a violation of good design practices. And good design practice is what made me start this thread in the first place!
    So, does a forward() nested in the body of a <jsp:useBean> qualify as a rubber mallet with an ergonomic handle?
    [ June 03, 2003: Message edited by: Philip Shanks ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to make MVC controller servlet the "default page"