tags:

views:

773

answers:

3

I'm writing a web application using JSP, and I really like to use the "XML style" JSP directive (i.e. <jsp:scriptlet> instead of <% ). But if I use generics in my code, the code content is not valid XML and I get compilation errors when the server (I use Jetty 6) tries to parse the file as XML.

For example, I want to write:

<jsp:scriptlet> 
List<String> myText = new List<String>();  
</jsp:scriptlet>

However this code obviously fails to compile. I tried adding CDATA wrapper around the code (immediately inside the jsp:scriptlet tag) but it doesn't look to make any difference.

Is it at all possible to use generics inside JSP scriptlets?

Thanks.

A: 

If you really want to write JSP, I'd aim to create tags (or use the standard tag library (or this) ) and keep the page clear of scriptlets - with or without generics. With proper MVC separation (or at least the view separated from the rest) you generally could use only the standard tag library, produce jsps that are easier to maintain and automatically validate as xml.

Are you intending to use any framework with your jsps or do you want to handle request processing within the jsp?

Olaf
Currently I'm retrofitting an existing system, and redesigning the architecture is way out of scope of this project :-)
Guss
But as you seem to want to get generics into the jsps, they currently seem not to be in there. Why not - instead of adding generics to the jsp - introduce tags? Back some years we had an accompanying class containing logic with every jsp in order to minimize code in jsps (when tags were difficult)
Olaf
There are generics in the current implementation, which uses old-style JSP code. I want to move as much as possible to XML-style code which is easier to read, validate and manage. The generics are often used for code that pertains only to the current page so abstracting it away is not option.
Guss
ok - understood. Too bad. (Though we used to pair a jsp with a class containing every non-outputting stuff at the time without any problem/drawback). Well - good luck!
Olaf
A: 

Does this page on jetty jsp configuration help at all?

Are your source and target language levels set to 1.5?

Ron
In Eclipse, yes (I'm actually using 1.6). The webapp is set as version 2.4 (not sure what it means).
Guss
+2  A: 
<jsp:scriptlet> 
    List&lt;String> myText = new List&lt;String>();  
</jsp:scriptlet>

I tried adding CDATA wrapper around the code (immediately inside the jsp:scriptlet tag) but it doesn't look to make any difference.

Really? That should also work. It's pretty ugly of course, but then no-one ever accused the JSP XML format of being convenient or anything...

bobince
I'm not sure what my problem was(is) - when I try to reproduce it on an empty page, it works great. Without CDATA I get a different error then what I get with the problematic page. Weird, but I'll let it go for now.
Guss
Yep, it was my fault - I'm stupid. The problem was that I tried to convert old style JSP which was not even well-formed XML to JSP 2.0 with XML syntax. As Jetty tried to parse my page with an XML parser, the XML parsing failed even though the syntax was correct.
Guss
Sadly there is no workaround like CDATA for attributes. I have just had this problem in type attribute of useBean. < can be replaced by < but then Eclipse's validation marks it as invalid...
calavera.info