Using a servlet for each view

Hi,

Is the following architecture a good one?

JSP View1 > FrontControllerServlet -> Servlet1 --> JSP View2

JSP View2 > FrontControllerServlet -> Servlet2 --> JSP View3

JSP View7 > FrontControllerServlet -> Servlet5 --> JSP View4

...

...

In other words, all the views that have Forms have their own controllers (servlets). The task of all the Servlets is to do validation and decide on the next view to display.

Any input is highly appreciated.

[534 byte] By [ranga_deva] at [2007-9-27 10:36:09]
# 1
That what I do but I call the Servlet1, Servlet2, etc helper instead of Servlets.
DanHinojosaa at 2007-7-9 1:07:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 2
As a follow up, my helper chooses which view to present next.
DanHinojosaa at 2007-7-9 1:07:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 3
Why don't you use Filters instead?
lgurevicha at 2007-7-9 1:07:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

You should try struts, which has the a similar model to yours, i.e. the MVC or Model 2.

Even better, they have the concept of FormBeans which you define to do your validation.

All you need to do is edit the XML configuration file to define the mappings between views to formbeans to action.

Create the formbeans.

And then create the JSP's.

You can find it at: jakarta.apache.org.

It's well documented and O'Reilly will be publishing a book on it around the corner and will even be included as part of Oracle's JDeveloper tools.

nicodemus_chana at 2007-7-9 1:07:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
How do you do extreme examples with XML mapping. For example, based on any given situation, I could forward my response to 20 different pages. How do you do that in Struts?
DanHinojosaa at 2007-7-9 1:07:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

Struts has the idea of an Action (read: servlet). Depending on the logic of the Action a different forward can be invoked. In the Action class, you access a Forward by a string name. The actual value of the forward (the page it will direct to) is specified in your config file. BAsically, Struts is great, but has a steep learning curve.

se95sna at 2007-7-9 1:07:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

I use one Servlet. That servlet then directs where the requests should go. This information can be passed in the request object as a hidden field from the HTML form.

For example. IF you are doing a logon, you could have a hidden field with a value of "DIRECT_FOR_LOGON". Then in your servlet, you get this parameter. If the value is "DIRECT_FOR_LOGON" you direct it to the logon code.

You don't have to limit to one servlet either, depending on the size of your application. So I guess, for similar tasks, you could have separate servlets...for example, logon, logoff, signup, etc. could have one Servlet director.

Plus, if you implement SingleThreadModel, you can ensure that your servlet is only handling one request at a time.

hemant_naidua at 2007-7-9 1:07:21 > top of Java-index,Other Topics,Patterns & OO Design...
# 8

The simple answer already provided is:

Use struts.

Some comments about the other posts:

1. You should have only one controler servlet, not more then one as sugested. It serves as the single entry point to your application and it decides what actions to take. If you have more then one servlet you are forced to take at least part of the decision already in the href linking to it, which would be a clear violation of the seperation of logic and display.

2. Actions in Struts are NO servlets. They have a reference to the Controlservlet invoking them in order to access the session object and the like. If they would be servlet you could address them directly from a webpage which doesn't make sense since they are supposed to get invoked by the controller servlet. For the same reason serlvetN in your design really shouldn't be servlets.

3. I don't agree to the steep learning curve thing. It took me about a day to get the concepts of struts, another day to make a little workshop with the rest of the team and after a week or so everybody was deeply involved in working with struts ...

regards

Spieler

just my two cents

spielera at 2007-7-9 1:07:21 > top of Java-index,Other Topics,Patterns & OO Design...