JavaServer Pages (JSP) and JSTL - Web development with J2EE, a rant

So, it had to happen...after years of trying without really trying, I am finally involved in a project that deals with J2EE Web development to the nth degree...

I took the project from the ground up with the intention of using all best practices and state-of-the-art design methods.

I knew about MVC Model 2, so that was not new to me.

I initially mapped Model-View-Controller to JavaBean-JSP-servlet.

I figured I would use all the right patterns:

- Front Controller

- Dispatcher View

- View Helper

- Intercepting Filter

- Value Object (JavaBean flavored)

Since I wanted to give the whole approach a fresh start, I ignored all heavy frameworks: Struts, JSF, Spring, WebWork...and the hundred or so other frameworks that average 30MB of bytecode.

So, I decided to roll up my own MVC "framework", to better understand the issues all other frameworks claim to and fail to resolve.

This post is just to get a discussion started, so I will now list a few things that drive me crazy:

- Front Controller

Why would you go through a central controller servlet to access a page that does not depend on any application logic?

Example: I have a couple of JSPs that contain template XHTML and access some application context parameters via EL.

Why would I force clients to request that page through a Front Controller?

- JSP/JSTL

"Scriplets are evil because Web designers don't know Java."

"Scriplets are evil because they introduce application logic in the presentation layer."

"Scriplets are evil because they make debugging very difficult."

Well, I kind of agree with all that, but what JSP/JSTL offer in exchange just doesn't seem any better to me.

- Model/JavaBean

I isolated most of my application logic in a separate library that includes a handful of packages.

This library contains nothing that is Web-centric.

Classes in this library define objects that encapsulate data and behavior that efficiently implement my application logic.

My front controller dispatches requests to command objects. These command objects use the library to do what they need to do.

Resulting state needs to be encapsulated into JSP/JSTL-friendly recipients, which means my command objects spend a lot of resources transferring data from my library objects into JavaBean objects, maps, attributes of primitive types...entities that are easily accessible via expressions, EL, standard tags, JSTL tags, etc.

- View

Following the wisdom of some textbook, I felt compelled to add a "getModel" API to my Command interface.

My front controller calls "getModel" after calling "execute" in order to determine which JSP the request should be forwarded to after command execution.

I don't really have a problem with this. The fact "getModel" always returns the filename of some JSP is only a minor annoyance that makes me think something is not quite right.

So, there we are for a start...

Am I the only one to think some of these things are not quite right?

[3129 byte] By [Dralta] at [2007-11-14]
# 1

Why would you go through a central controller servlet to access a page that

does not depend on any application logic?

For consistency.

You have a central point of entry that you can control

If you DO need to change this page and make it more complex at some point, so that it DOES depend on application logic, it is already in the standard form.

Well, I kind of agree with all that, but what JSP/JSTL offer in exchange just

doesn't seem any better to me.

I disagree with this statement, particularly if you have to mix a for loop in code, with a table. Particularly if it is nesting <% { %> <% } %> and html.

Resulting state needs to be encapsulated into JSP/JSTL-friendly recipients

What sort of conversion is necessary?

How much of it? I normally find that the bean hierarchies I define are very open to being accessed by JSTL / EL.

On the whole it sounds like you're doing ok.

I would probably have been against inventing the wheel (again). I would recommend taking a look at Struts for instance, and see how they solved the similar issues, or if they explain why they followed the certain patterns.

Cheers,

evnafets

evnafetsa at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 2
Thanks, I'll study Struts for answers.I will also try to post examples of what I mean.
Dralta at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 3

Why would you go through a central controller

servlet to access a page that

does not depend on any application logic?

For consistency.

You have a central point of entry that you can

control

If you DO need to change this page and make it more

complex at some point, so that it DOES depend on

application logic, it is already in the standard

form.

OK, let's say my front controller is Controller.

I map *.do to Controller.

I have a display_user_record.jsp page and a login.jsp page.

The link for the display_user_record.jsp page is something like "PrepareUserRecord.do".

"PrepareUserRecord" is the command class that handles that request. display_user_record.jsp is the resulting view.

That looks nice and work well because displaying that page requires fetching data from a database and some other logic.

For the login.jsp page, which requires no application logic, what would the link be like? What would would happen in the controller?

Clearly, no matter what happens, accessing login.jsp directly would achieve the same effect.

Well, I kind of agree with all that, but what

JSP/JSTL offer in exchange just

doesn't seem any better to me.

I disagree with this statement, particularly if you

have to mix a for loop in code, with a table.

Particularly if it is nesting <% { %> <% } %> and

html.

Well, in many cases, you end up with things that look like this:

<exec:iterator condition=?lt;%= user.isActive() %>?gt;

It's barely better than Java code.

Add in page directives, directives accessed using the <jsp:directive tag, declarations, expression, EL expressions, an occasional scriptlet, implicit objects, JSTL tags from 2 or 3 different namespaces, standard JSP tags, EL functions, some custom tags...and you end up with something really scary...for a Web designer and a developer.

<div class="jive-quote">

Resulting state needs to be encapsulated into

JSP/JSTL-friendly recipients

What sort of conversion is necessary?

How much of it? I normally find that the bean

hierarchies I define are very open to being accessed

by JSTL / EL.

Well, my library classes are heavy, they contain a lot of processing logic that has no place in the presentation layer. Also, they are almost never beans.

So, I have this whole range of JavaBean classes whose sole purpose is to contain the state of library classes that is necessary and sufficient to my views.

Every time I extract data from my library objects to populate those JavaBean objects, I have the feeling of doing something very inefficient: Creating short-lived host objects for the sole purpose of making the data accessible by the presentation layer soup of tags and EL.

I guess I am looking for an elegant solution and it always end up very messy.

I wish there were a good book or reference on that topic.

Installing 30MB of bytecode just for a MVC framework is crazy as far as I am concerned.

Message was edited by:

Dralt

Dralta at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 4
Now with Duke Stars to stimulate the debate... :)
Dralta at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 5

<exec:iterator condition=?lt;%= user.isActive() %>?gt;

It's barely better than Java code.

In EL, you'd say that like

 <exec:iterator condition=?{user.active}">

I know where you're coming from. I blew off JSTL as nothing more than "scriptlets 2.0" for years. But after using it for a month, I'll never go back. JSTL makes your JSP an order of magnitude easier to maintain. And since it only exposes a subset of the Java language, it helps to enforce the standard that JSP's are for view only.

bckrispia at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...
# 6

I know where you're coming from. I blew off JSTL as

nothing more than "scriptlets 2.0" for years. But

after using it for a month, I'll never go back. JSTL

makes your JSP an order of magnitude easier to

maintain. And since it only exposes a subset of the

Java language, it helps to enforce the standard that

JSP's are for view only.

Oh, clearly, I am all in favor of keeping code out of my JSP.

I'm only ranting about the fact we ended up with such a messy bunch of mechanisms.

Mechanisms which are not strictly equivalent. Examples: 1) Page directives allow you to do things jsp:directive cannot. 2) The <%@ include directive is not equivalent to the <jsp:include tag. 3) EL overlaps and extends prior mechanisms. Etc. The list goes on and on...

When I first researched this whole project, I felt like a palaeontologist discovering strata of successive failed attemtps at solving the problem.

Message was edited by:

Dralt

Dralta at 2007-7-10 > top of java,Enterprise & Remote Computing,Web Tier APIs...

Enterprise & Remote Computing Hot Topic

Enterprise & Remote Computing New Topic

Enterprise & Remote Computing

Site Classified