Home > Archive > Java Help > October 2005 > Implementing a Controller Servlet: forward to another servlet without cluttering up w
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Implementing a Controller Servlet: forward to another servlet without cluttering up w
|
|
| Alex Meier 2005-10-28, 7:02 pm |
| Hi, NG
if I implement a simple Controller Servlet, I find myself having to
define each "action" which the controller forwards to, in the WEB-
INF/web.xml of tomcat, cluttering it up.
How do I manage the classes to which I want to dispatch (forward) to, to
be found without adding them to web.xml?
To illustrate this a little bit - my controller prototype is made up
like this:
//--------------------------------------------------------------------
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException {
String Action;
action = request.getParameter("action");
// forward to the passed action
RequestDispatcher dispatcher = request.getRequestDispatcher(action);
dispatcher.forward(request, response);
}
//--------------------------------------------------------------------
I want the Controller Servlet to be the only servlet defined in the
web.xml. But if I try to call the controller with the HTTP parameter
"action=Hello", Tomcat nags "The requested resource (/webapp/Hello) is
not available.".
If I add the servlet Hello to web.xml I am able to call the controller
using the HTTP parameter "action=Hello" and all goes well, i.e. the
servlet Hello.class is being executed and the output is displayed.
How can I avoid this?
Andi
--
remove underscore+nospam if you want to mail me
| |
|
| On Fri, 28 Oct 2005 21:05:34 +0200, Alex Meier
<superdoehli_nospam@gmx.at> wrote:
>if I implement a simple Controller Servlet, I find myself having to
>define each "action" which the controller forwards to, in the WEB-
>INF/web.xml of tomcat, cluttering it up.
Why not? That's what the web.xml is for.
Or, you can just define the "action" class from an interface, use the
action=<SomeClass> parameter, and instantiate the "SomeClass" class,
and delegate to it... Not perfect, but that's what we do (or use
something like Struts which has that sort of thing built in, but then
you'll have to "clutter" up the struts config file...)
--
now with more cowbell
| |
| Alex Meier 2005-10-29, 7:57 am |
| In article <et15m151e000qi6f222p68u3o8qc04nbah@4ax.com>,
spamtrap@berzerker-soft.com says...
> On Fri, 28 Oct 2005 21:05:34 +0200, Alex Meier
> <superdoehli_nospam@gmx.at> wrote:
>
>
> Why not? That's what the web.xml is for.
Hm, I don't agree in this case. The web.xml is IMHO primarily for
publishing servlets, i.e. make them visible to an outside request.
What I want to do is something different: I want my action servlet NOT
to be visible to outside requests. Only the Controller servlet should be
able to be invoked by an external request. The Controller servlet itself
dispatches the request to the appropriate action classes.
When I include all action classes (which in turn are servlets?) in the
web.xml, everybody could just invoke directly these servlets and
circumvent my Controller. To me, this is no clean architecture.
So I'm looking for a way to make the action classes NOT visible to
external requests but OTOH let them be able to be forwarded to by the
Controller class.
> Or, you can just define the "action" class from an interface, use the
> action=<SomeClass> parameter, and instantiate the "SomeClass" class,
> and delegate to it... Not perfect, but that's what we do (or use
> something like Struts which has that sort of thing built in, but then
> you'll have to "clutter" up the struts config file...)
Do you know...is this how Struts does it?
Andi
--
remove underscore+nospam if you want to mail me
| |
|
| On Sat, 29 Oct 2005 12:31:34 +0200, Alex Meier
<superdoehli_nospam@gmx.at> wrote:
>
>Do you know...is this how Struts does it?
Yes, struts uses a configuration file to determine what Action class
to invoke. Except Struts (by default) uses an extension on the URL to
determine the action, and looks this action up in the Struts Config
file to determine the class to delegate to.
i.e.,
http://www.somedomain.com/myapp/myaction.do
if it ends in .do, then it looks for myaction in the config file.
--
now with more cowbell
|
|
|
|
|