Catch 22: FrontController, Infinite Loops, and Static Resources

49 views
Skip to first unread message

Mark van Wyk

unread,
May 5, 2011, 2:31:06 PM5/5/11
to ctjug...@googlegroups.com
Hey friends,

I created a simple FrontController. It takes a request, does some neat stuff to it, and then passes it on to a JSP. 

So of course you map

/* to com.foxbomb.portal.ControllerServlet, which does some neat shit, and forwards the request onto /views/*

Which causes an infinite loop.

Yeah yeah, I can prefix it with /go, /action, or make *.html map to *.jsp but I think that sucks. I want .jsp to map to .jsp and I don't want a prefix. Any suggestions.

Another question... Of course, you have your /static where you store your images, etc.

What do you think is the best was to overcome this. I saw someone mapping /pages and /static to default which (apparently on Tomcat and Jetty) maps it to the normal java server - but this is then container specific, so that sucks too.

Any brilliant ideas appreciated...

Mark van Wyk



Mobile: 082 831 9227

Andre Roodt

unread,
May 5, 2011, 2:42:49 PM5/5/11
to ctjug...@googlegroups.com
How about one of JSF / Struts / Wicket / Seam / Tapestry / GWT / Stripes or any fweb framework that you dont have to create yourself. Just saying :-)

--
You received this message because you are subscribed to the Google Groups "CTJUG Forum" group.
To post to this group, send email to CTJUG...@googlegroups.com
To unsubscribe from this group, send email to CTJUG-Forum...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/CTJUG-Forum
For the ctjug home page see http://www.ctjug.org.za
For jobs see http://jobs.gamatam.com/

Mark van Wyk

unread,
May 5, 2011, 2:52:51 PM5/5/11
to ctjug...@googlegroups.com
Hi Andre,

My needs are sooooo simple. I really just want to pull a few things out the headers and store them in a database, not create a full MVC application.

Thanks,

Mark van Wyk



Mobile: 082 831 9227



Chris Oloff

unread,
May 5, 2011, 3:03:03 PM5/5/11
to ctjug...@googlegroups.com
Sounds like your FrontController could be a javax.servlet.Filter?

Chris
Christian Oloff

christi...@googlemail.com (email)
christian.oloff (skype)

Mark van Wyk

unread,
May 5, 2011, 3:30:52 PM5/5/11
to ctjug...@googlegroups.com
Snap, I just got the filter working. It seems that a filter isn't prone to a loop like a servlet is. You can dispatch "past" the filter. Yay! I'll post the code later.

Thanks for your input!

Mark van Wyk



Mobile: 082 831 9227



Chris Oloff

unread,
May 6, 2011, 6:09:31 AM5/6/11
to ctjug...@googlegroups.com
You're welcome!

According to email timestamps you did the filter in less than 28 minutes, but posting the code seems to take significantly longer... hint hint ;)

Cheers,
Chris

Mark van Wyk

unread,
May 6, 2011, 6:44:00 AM5/6/11
to ctjug...@googlegroups.com
For those interested. A servlet filter did the trick perfectly. I passed 2 parameters via web.xml to the filter, one telling it to happily pass on /static urls to the /static folder, and the other telling it to append /controller in front of anything else (that needs to be dealt with by the controller).

This way, we're dealing with all requests on a root (/*.*) level, static stuff is passed on to /static/ and everything else is forward dispatched to /controller/original-path.

I then have a Servlet mapped to /controller/* that deals with everything. Example

/static/picture.jpg -> Filter -> /static/picture.jpg
/special-action.jsp -> Filter -> /controller/special-action.jsp -> Servlet (add headers, save headers, forward to) -> /pages/special-action.jsp

Works like a charm... well almost... For some reason if the request.getDispatcher("/this/that") returns a 404, the dispatcher gets stuck in an infinite loop. Investigating further...

First, web.xml, so that the context makes sense:

<?xml version="1.0" encoding="UTF-8"?>
<filter>
<filter-name>RequestFilter</filter-name>
<filter-class>com.foxbomb.mxportal.Filter</filter-class>
<init-param>
<param-name>staticPath</param-name>
<param-value>/static</param-value>
</init-param>
<init-param>
<param-name>controllerPrefixToAppend</param-name>
<param-value>/controller</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>RequestFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>com.foxbomb.mxportal.Controller</servlet-class>
<init-param>
<param-name>viewsPath</param-name>
<param-value>/views</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/controller/*</url-pattern>
</servlet-mapping>
</web-app>

Next, the filter code:

public class Filter implements javax.servlet.Filter {

private static final String PARAM_STATIC_PATH = "staticPath";
private static final String PARAM_CONTROLLER_PREFIX_TO_APPEND = "controllerPrefixToAppend";

private FilterConfig filterConfig = null;

public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
String staticPath = filterConfig.getInitParameter(PARAM_STATIC_PATH);
String controllerPrefixToAppend = filterConfig.getInitParameter(PARAM_CONTROLLER_PREFIX_TO_APPEND);

String path = getPath(request);

request.setAttribute(PARAM_CONTROLLER_PREFIX_TO_APPEND, controllerPrefixToAppend);
request.setAttribute("path", path);

if (path.startsWith(staticPath)) {
request.getRequestDispatcher(path).forward(request, response);
} else {
request.getRequestDispatcher(controllerPrefixToAppend + path).forward(request, response);
}
}

public void destroy() {
}

private String getPath (ServletRequest request) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
int contextLength = httpRequest.getContextPath().length();
String uri = httpRequest.getRequestURI();
return uri.substring(contextLength);
}
}

I'll spare toy the details of the Controller - still in progress...

;)

Mark


Mark van Wyk



Mobile: 082 831 9227



Gerrit

unread,
May 6, 2011, 7:14:18 AM5/6/11
to ctjug...@googlegroups.com

I’m not sure what the controller does, but if you are keen on sharing the final product I would be very interested.

 

I did something sort of similar a while back (also servlet based) where I wanted to avoid the mappings etc.

I used introspection/reflection in the filter to find the appropriate action classes directly. Ie /person/add url would redirect through my filter initializing the person action class, calling the add method and finally passing the request along to the dispatcher. Worked like a charm, no middle men, create your action class and call it via your url’s.

This did however cause some security issues which were easily overcome via adding some checks to the filter.

Chris

 


Mark van Wyk

 

Error! Filename not specified.

 

Mobile: 082 831 9227



On 5 May 2011 20:42, Andre Roodt <aro...@gmail.com> wrote:

How about one of JSF / Struts / Wicket / Seam / Tapestry / GWT / Stripes or any fweb framework that you dont have to create yourself. Just saying :-)

On Thu, May 5, 2011 at 8:31 PM, Mark van Wyk <ma...@foxbomb.com> wrote:

Hey friends,

 

I created a simple FrontController. It takes a request, does some neat stuff to it, and then passes it on to a JSP. 

 

So of course you map

 

/* to com.foxbomb.portal.ControllerServlet, which does some neat shit, and forwards the request onto /views/*

 

Which causes an infinite loop.

 

Yeah yeah, I can prefix it with /go, /action, or make *.html map to *.jsp but I think that sucks. I want .jsp to map to .jsp and I don't want a prefix. Any suggestions.

 

Another question... Of course, you have your /static where you store your images, etc.

 

What do you think is the best was to overcome this. I saw someone mapping /pages and /static to default which (apparently on Tomcat and Jetty) maps it to the normal java server - but this is then container specific, so that sucks too.

 

Any brilliant ideas appreciated...

 

Mark van Wyk

 

Error! Filename not specified.




--

Mark van Wyk

unread,
May 6, 2011, 7:26:09 AM5/6/11
to ctjug...@googlegroups.com
Wow!

I'd love to see that.

Let me understand that correctly:

Classloader loads all action classes.

You visit /person/add

Will find and invoke the Person class and call the add method automatically.

Perhaps then ask Person for getView() and forward to the appropriate view.

So, basically, you create a couple of smart annotated classes, a couple of views and you're done.

I WOULD LOVE TO SEE THAT!!

Well done!

When we hooking up?


Mark van Wyk



Mobile: 082 831 9227



Mike

unread,
May 6, 2011, 9:35:52 AM5/6/11
to ctjug...@googlegroups.com
On 06/05/2011 13:26, Mark van Wyk wrote:

> So, basically, you create a couple of smart annotated classes, a couple
> of views and you're done.

One step better - you don't even have to define any views.
http://incubator.apache.org/isis/index.html

--
mike morris :: mikro2nd (at) gmail (dot) com

http://mikro2nd.net/
http://mikro2nd.net/blog/planb/
http://mikro2nd.net/blog/mike/

This email is [X]bloggable [ ]ask-first [ ]private

Mark van Wyk

unread,
May 6, 2011, 10:19:12 AM5/6/11
to ctjug...@googlegroups.com
Automatically generated UI's? From what? Using what? How?


Mark van Wyk



Mobile: 082 831 9227



Mike

unread,
May 6, 2011, 11:09:16 AM5/6/11
to ctjug...@googlegroups.com
On 06/05/2011 16:19, Mark van Wyk wrote:
> Automatically generated UI's? From what? Using what? How?

Using the object model itself, naturally. Objects are the nouns; methods
are the verbs; you annotate for better UI.

Of course this means that you actually have to build a REAL object
model, which may confound a bunch of those JEE weanies...

Mark van Wyk

unread,
May 6, 2011, 12:11:15 PM5/6/11
to ctjug...@googlegroups.com
Can I come get a demo?

Mark van Wyk



Mobile: 082 831 9227



Herman Lintvelt

unread,
May 7, 2011, 9:04:39 AM5/7/11
to ctjug...@googlegroups.com
"Of course this means that you actually have to build a REAL object
model,"

Oh no! Don't tell me I'l have to do proper OO thinking... and not just hack by clicking random buttons in the tooling...
;-)





Mike

unread,
May 7, 2011, 9:13:04 AM5/7/11
to ctjug...@googlegroups.com
On 06/05/2011 18:11, Mark van Wyk wrote:
> Can I come get a demo?

With pleasure. R highly neg. ;-)

Mark van Wyk

unread,
May 7, 2011, 9:42:27 AM5/7/11
to ctjug...@googlegroups.com
O'll pay you in ego-stroking ;)

Mark van Wyk



Mobile: 082 831 9227



Bobby Quinne

unread,
May 8, 2011, 9:15:42 AM5/8/11
to ctjug...@googlegroups.com
On Thu, May 5, 2011 at 8:52 PM, Mark van Wyk <ma...@foxbomb.com> wrote:

My needs are sooooo simple.

With the bevy of emails you have been sending to the group, your statement is contrary to your actions.
 



--
If you have something tough, give it to the Americans. If you have something difficult, give it to the Indians. If you have something impossible, give it to the Russians

Mark van Wyk

unread,
May 8, 2011, 12:34:30 PM5/8/11
to ctjug...@googlegroups.com
Oops,

Sorry I've been sending too many emails. Just get excited and love to have other Java developers to collaborate with - especially that I work from home.

Will pipe down a little.

Thanks,

Mark van Wyk



Mobile: 082 831 9227



Dr Heinz M. Kabutz

unread,
May 8, 2011, 1:27:58 PM5/8/11
to ctjug...@googlegroups.com
Guys, humour the only extrovert in the group ;-)
Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
IEEE Certified Software Development Professional
http://www.javaspecialists.eu
Tel: +30 69 72 850 460
Skype: kabutz 

Louis Botes

unread,
May 8, 2011, 1:41:30 PM5/8/11
to CT Jug
I would be more talkative if I had more to add... But for now I am reading and learning...

Sent via my CrackBerry...


From: "Dr Heinz M. Kabutz" <he...@javaspecialists.eu>
Date: Sun, 08 May 2011 20:27:58 +0300
Subject: Re: [CTJUG Forum] Catch 22: FrontController, Infinite Loops, and Static Resources

Dr Heinz M. Kabutz

unread,
May 8, 2011, 1:52:31 PM5/8/11
to ctjug...@googlegroups.com
Extroverts usually don't wait until they have something to add before they talk ... ;-)  No problem, Mark, keep them coming.

Regards

Heinz
-- 
Dr Heinz M. Kabutz (PhD CompSci)
Author of "The Java(tm) Specialists' Newsletter"
Sun Java Champion
IEEE Certified Software Development Professional
http://www.javaspecialists.eu
Tel: +30 69 72 850 460
Skype: kabutz 


For jobs see http://jobs.gamatam.com/ --

Louis Botes

unread,
May 8, 2011, 2:00:48 PM5/8/11
to CT Jug
Personally the odd bit of random banter outside of the world of java does not bother me the least little bit...

Slightly related. I just hired a new junior Dev. It is heart breaking how few candidates I interviewed which knew what a database index was. Even more so how few could outline the difference between a class and an interface.

Sent via my CrackBerry...


From: "Dr Heinz M. Kabutz" <he...@javaspecialists.eu>
Date: Sun, 08 May 2011 20:52:31 +0300
Reply all
Reply to author
Forward
0 new messages