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

design help

Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
Note: since this got long, feel free to skip the first two paragraphs to get at the meat of my question. Thanks!

I've read up on a lot of J2EE technologies in hopes of improving the web app I'm in charge of at work, but I think it would be quite a while before ours would be large enough to warrant a redesign using EJBs, etc (though this is just my amateur guess). Right now it's very JSP-centric (in fact, there are no explicit servlets and I'm considering adding some right now). This is a source of great frustration to me...since the JSPs post to JSPs and interact with Java Beans, it seems like the code is very messy and sometimes it seems harder than it should be to add to or modify exising functionality. I know that adding servlets wouldn't really clean it up as much as EJBs, but I think it would help a lot if posts went to servlets and they interacted with the Java Beans and then forwarded on to JSPs.

In fact, this is what Model 2 shows we should do, but in looking around online I don't see many examples of that...usually it's either a slightly simpler model or full-blown EJBs thrown in. Maybe the book I have isn't so great (Kurniawan's Java for the Web), but it suggests having a single controller servlet that just redirects to different JSPs (maybe I'm misinterpreting this...other places online suggest this as well, so I'm wondering if they mean to have a single controller servlet for every needed action).

To me it seems like I should essentially have a servlet, Java Bean and JSP for each thing a user needs to do (and possibly a little less when it makes sense to reuse or use a JSP template instead). The original developer reused a Java Bean for several JSPs in the site, and this seems like it could get messy, especially since the bean is designed to load and hold a list of items for display or processing and if multiple logins for the same username occurred, they could start interrupting each other mid-process since it's the Java Bean is a session bean.

Anyway, am I missing something when I say it seems wrong to have a single Controller servlet for all actions in our web app?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60057
    
  65

In fact, this is what Model 2 shows we should do, but in looking around online I don't see many examples of that.


Maybe you need to look harder. Every app I work on works that way. And besides, just because others may be behaving badly, should you?

Listen to your heart. Model 2 will make you a better person.

To me it seems like I should essentially have a servlet, Java Bean and JSP for each thing a user needs to do


That might be a bit parochial. For example just one JavaBean? What if it makes sense to have more than one, or seven? Don't fall down the trap of creating a page-specific JavaBean pfor each JSP that is the be all and end all of everything a page needs to do. Take what the page needs to do and abstract into as many beans as make sense. Think abstraction and reuse.

Anyway, am I missing something when I say it seems wrong to have a single Controller servlet for all actions in our web app?


That's one possible pattern. Another is the Front Controller where a single servlet is used to intercept all requests, performs any common processing, and invokes an "action" that performs the specific task; the action to take being identified by details of the URL. This is the design pattern used by frameworks such as Struts (the subject of your other post about ".do").

For example, my apps use a front controller mapped to URLS of the form:

http://servername.com/webappname/action/whatever

the "action" part maps to the front controller, which uses the "whatever" part to instantiate an action of the appropriate type and execute it.

This approach allows a lot of common processing (such as converting request parameters into digestible types as appropriate) without a lot of code repitition.

Of course the same can be achieved by putting the common processing into an abstract base servlet and extending it for your individual controllers; another valid pattern (though I prefer the front controller for various reasons).

In short, don't be lead down the JSP-centric path (Model 1) just because you see a lot of it around you. You can do better.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
You may probably like to have a look at the MVC framework (Struts). This will help you reuse most of yor bean components. You will only have to move your business logic to the action classes.

To start with have look at the given links:
http://www.projectrefinery.com/struts.html
http://jakarta.apache.org/struts/index.html
Stephen Huey
Ranch Hand

Joined: Jul 15, 2003
Posts: 618
You know, I read up on Struts not too long ago, and I stumbled upon Tapestry this past weekend, and it's frustrating because I wish there was a best way, but yeah, I know this is the real world we're talking about. Anyway, I decided to take a step back...we have a live application in use by tons of people, and while it's not as large as some enterprise systems, I know it shouldn't be so JSP-centric. However, I'm really not sure it actually needs to be done with Struts or Tapestry, either. That's why I was trying to take a step back and figure out the best way to do it with just servlets, beans and JSPs, and in limiting myself to that model, I've gotten confused by some things like whether to have a single servlet or multiple servlets.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: design help
 
Similar Threads
Specifying new J2EE project - opinions wanted
jsp vs servlets vs. beans
In MVC, who stores the business logic
Please help me in design
Question about EJBs and JSP/Servlets