aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes JSP Documents, EL and CDATA construct Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "JSP Documents, EL and CDATA construct" Watch "JSP Documents, EL and CDATA construct" New topic
Author

JSP Documents, EL and CDATA construct

Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Howdy Ranchers!

In Charles Lyons book, "SCWCD Study Companion" in Chapter 12, Question 18 you can read:

What is the result of compiling and executing this JSP document? (choose one):



A. The text 'Application title: ' followed by the value of the context parameter title is written to the response.
B. The text 'Application title: ${ context['title'] }' is written to the response.
C. The text '<![CDATA[Application title: ${ context['title'] }]]>' is written to the response.
D. A translation error occurs.


The given answer is B.

I've tested this kind of example in Tomcat 6 (and in Tomcat 7 by the way) and it gives me the 'Application title: ' result.

Isn't the EL parser running before the code is translated into an XML View? I mean, isn't the EL executed before the CDATA can do its work?

In my opinion none of the answers are correct, as the A (which was my choice) is not correct because of the "context parameter title". The context (init) parameters can be read through initParam map - not context map, right?

By the way, when I try to type the exact code as written, I am not able to run it. I guess it's because the old <jsp:root...> element. If I change it to the <html ...> it runs fine, i.e.:



is OK, but


is giving me an XML parsing error: syntax error in line 1, column 1:
test:
^

Cheers!


OCP Java SE 6 Programmer, OCM Java SE 6 Developer, OCE Java EE 6 JSPSD, OCE Java EE 6 EJBD, OCE Java EE 6 JPAD, Spring 3.0 Core Professional.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

A JSP document is an XML document, so (although I am ignorant of these details) I would assume that it is parsed by an XML parser. Which of course would handle the CDATA section in the normal way for an XML parser.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Thanks Paul, but than again - what is the normal way for an XML parser to evaluate EL expression? I mean:



will display Howdy Ranchers instead of Howdy ${ "Ranchers" }. In my opinion, the XML parser goes in action after the EL got evaluated and not before.

Therefore, isn't the option B in question quoted in the first post incorrect?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

XML parsers don't evaluate EL expressions. They don't know anything about EL. They would simply treat them as text and leave them unchanged.

So you're suggesting that something takes that XML document and preprocesses it by evaluating all of the EL expressions, and then it's given to an XML parser to deal with XML constructs like CDATA and character entities and so on? That seems highly improbable to me, if only because evaluating the EL expressions could easily cause the XML document to become ill-formed.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

So, what and when evaluates the EL in JSP Document?

As I understand it is evaluated before it is executed, right?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

My assumption would be that there's a "JSP compiler" which takes the result of parsing the XML document and produces a servlet which is actually executed. That at least appears to be consistent with what you see.
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Paul Clapham wrote:(...)So you're suggesting that something takes that XML document and preprocesses it by evaluating all of the EL expressions, and then it's given to an XML parser to deal with XML constructs like CDATA and character entities and so on? That seems highly improbable to me, if only because evaluating the EL expressions could easily cause the XML document to become ill-formed.


Well, I think you're right Paul. If my theory would be correct (that the EL runs before XML parser), than I guess the following code:


should produce valid JSP Document, just like this one:


right? Well, it doesn't...

So, the XML Parser runs and checks if the JSP document is a valid XML structure, and then the EL gets evaluated, right? Hence, that's why the following code is incorrect:
(it throws an exception: org.apache.jasper.JasperException: /jspdoc.jspx(2,16) The content of elements must consist of well-formed character data or markup.)


while the following is correct (no translation error):


But it doesn't change the fact that it produces invalid XML document...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JSP Documents, EL and CDATA construct