aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Scripting in Simple Tag - Head First question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Scripting in Simple Tag - Head First question" Watch "Scripting in Simple Tag - Head First question" New topic
Author

Scripting in Simple Tag - Head First question

Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Howdy Ranchers!

I have a doubt about question 17, chapter 10 from Head First book by K&B (pp. 598).

Given a tag, simpleTag, whose handler is implemented using the Simple tag model and a tag, complexTag, whose handler is implemented using the Classic tag model.
Both tags are declared to be non-empty and non-tag dependent in the TLD. Which JSP code snippets are valid uses of these tag? (Choose all that apply.)

C.


The answer is:

-Option C is correct because the include directive is processed before the body of the simpleTag is converted into a JspFragment; however, the included content must also be non-scripting (which is why this example includes an HTML segment).


And I did agree with that statement, as I remembered that a JspFragment (its body-content) can be empty, tagdependent or scriptless but not JSP. So it is not allowed to put scriptlets like:



Ok, so I did try to test it in following way:

1. pedroTags.tld descriptor which consists of:


2. PedroTag.java file:


3. result.jsp file which consists of:



4. test.jsp file which consits of:



As you can see, the test.jsp does include some pure-evil scripting parts. I thought that:
1. result.jsp sees the include directive, so it puts the test.jsp result just as it is - without including it in the terms of RequestDispatcher.include(-).
2. At this point we should have result.jsp with a <pedro:myTag2> which consists of some scriptings. It is a violation of the body-content="scriptless" definition in TLD.
3. Exception should be thrown that the JSP doesn't allow the <% structure in this place (<pedro:myTag2>)

But in fact this works perfectly and it is the result I get:



----
It seems that the test.jsp is compiled and the result.jsp is including the RESULT of the test.jsp compilation. Is this what happened?

Am I wrong in my understading?

Thanks in advance for your help!


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.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1696
    
  25

Hi Pedro,

Interesting topic, you must be near taking your exam
It seems that the test.jsp is compiled and the result.jsp is including the RESULT of the test.jsp compilation. Is this what happened?

Yes, it seems that the included jsp (test.jsp) is first compiled before the rest of the result.jsp is compiled. If you look at the generated code and you look at the generated code of the result.jsp if you put the <%@ include file...> outside of the simple tag it looks very simular.

According to the specs, in the translation phase:
The container interprets the standard directives and actions, and the custom actions referencing tag libraries used in the page.

The "interpretation" of the standard directive <%@ include file...> by tomcat removes all the scriptlet code from test.jsp and that is why there is no problem compiling the result.jsp. However the exact order of events that should take place during the translation phase is not mandated by the specs.

I have tested the same setup on Jboss and it works exacly the same way.

Regards,
Frits
Piotr Nowicki
Ranch Hand

Joined: Jul 13, 2010
Posts: 610

Hi Frits,

Yea, indeed, I almost finished the HF book, but I need to wait for JEE 6 exams to be available and also study some spec differences between JEE 5 and JEE 6 :-)

So, to sum up: this is a nice feature when you consider Tomcat but it must not be relied on, as it is not supported according to the spec.

Thanks a lot Frits for your time and explanaition :-)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Scripting in Simple Tag - Head First question