This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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!
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
Joined: Oct 15, 2002
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?
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.
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
"Write beautiful code; then profile that beautiful code and make little bits of it uglier but faster." --The JavaPerformanceTuning.com team, Newsletter 039.
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
...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
Joined: Dec 19, 2002
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!
Joined: Oct 15, 2002
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 ]