Context, Attributes and JSP

Posted on Thu 10 July 2008 by alex in geek

One thing that is rapidly becoming clear is a lot of different concepts get mixed together when you start playing with JSP. On the face of it it's a simple model, your JSP code get built into a Java class (of type servlet) which is a combination on normal Java code and JSP mark up which gets converted into Java when the page is loaded. At any point in your HTML you can escape back into Java code, do calculations, execute loops, generally compute things. However that approach can get very messy because you end up spreading logic all through your presentational HTML. This is why using an MVC pattern is so encouraged. Put all your logic and complex code at the top of the file and then display page expanding the odd attribute here and there.

However I don't think I've fully grasped whats going on behind the scenes. It doesn't help with Jetty I can't find where the intermediate Java source code is generated. Trying to understand whats going on when I'm not familiar with the cryptic backtraces has been frustrating to say the least.

Now I'm a little confused as to what exactly happens with the ${var} notation in terms of the java code generated. My earlier code has mutated to a list with a custom class. Hence I have had to generate the following:

<c:forEach var="entry" items="${model.jvmMemInfo}" varStatus="status">          <%               // This is complex, I'm not sure how you can use ${} syntax on               // arbitrary objects to expose the values, hence this bit               // of inline java. I'm open to elucidation on how you would do so               NameValuePair data = (NameValuePair) pageContext.getAttribute("entry");             %>             <tr>               <td>          <%= %>               </td>               <td>           <%= data.value %>               </td>             </tr>           </c:forEach>

However there are examples on the net showing arbitrary classes exposing their contents with this simple ${} type syntax. I just couldn't get it to work. My NameValuePair class isn't much different from their one. I suspect this would be a lot easier to work out if I could see the generated applet.

Any Java web monkeys got experience with Jetty or can tell me what piece of obviousness I have missed?