The current application I’m working on is a traditional J2EE application from 2003!, with a JSP based front-end, and some service tiers that are the usual suspects (Auth, DAOs etc.).
Trouble is, the front-end is a mess. No MVC, no templating, no nothing. Developers over the years have written code straight into JSPs (I mean massive chunks of Java code), and at some point in those chunks, the control flow passes into the service tiers. It’s a pain to read and debug.
I’m writing new functionality for this application, and have to do it in a fairly unobtrusive way. Anything that changes xml configs, or classpath settings etc., is generally discouraged.
My question is: Given nothing but JSPs as entry points into the application, how to best go about writing the cleanest possible code under these constaints?
I’m making ajaxian modules (jQuery available in project svn already), so my JavaScript and JSPs typically look like these:
JavaScript:
jsAjaxCall({
url: 'myActionURL.jsp',
method: 'post',
params: {},
success: function(data){
},
failure: function(data){
}
});
myActionURL.jsp
<%
//entry into server tiers
SomeEntryPointClass entry = new SomeEntryPointClass(request);
String responseString = entry.doYourJob();
PrintWriter out = response.getWriter();
out.print(responseString);
out.flush();
%>
Questions:
- The idea is to keep Java Code inside JSPs at an absolute minimum,
and handoff the request object to a front-facing (what an thing to
name it!) object as soon as possible. Is this a reasonably correct
approach given the constraints? - What are the additional things I can do to keep things clean and
well organized? (I’ve already rolled my own classes for binding
request parameters to domain objects, so that’s a nice plus too). - How do I do PAINLESS form validation in a situation like this? I’m
missing Spring MVC’s validation! Just can’t imagine all the manual hoopla and routing and binding mess with nothing but JSPs! 🙁
The good news is that jsp’s compile into servlets. This ofcourse is good news as it will allow you to use your jsp’s in the way you would normally use servlets.
Keeping your java usage in the jsp’s and passing off to helper objects ( couldn’t bring myself to say front facing) is the correct call as:
1) There is a hard limit to how big your jsp files can get
2) writing unit tests from jsps is harder than pure classes
3) most IDEs won’t give you the same level of code writing support for java embedded in a jsp that they do for normal classes
4) debugging will be easier
As for your second question if it is jsp 2 you could look at using custom tags, they can be a useful way to organise jsp heavy code.
Additionally I guess if you want to go ajax the whole way and are feeling cunning you could write jsp pages designed to provide you Json objects. Effectively turning your jsp’s into front end services.
As for the third option, I’m sorry but your system sounds anything but painless.
With strut’s or jsr303 or spring mvc or even new servlets validation is going to be a bit painful.
Ofcourse if you wrote a validation jsp which returned a validated json object (see suggestion above) it might make your life a little easier.
Good luck