File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Scripting with JSTL Core tags. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Scripting with JSTL Core tags." Watch "Scripting with JSTL Core tags." New topic
Author

Scripting with JSTL Core tags.

Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
I read that:
" JSTL tags expose information as JSP EL variables. When you want to use an EL variable exposed by a JSTL tag in an expression in the page's scripting language , you use the standard JSP element jsp:useBean to declare a scripting variable.
For example:


So that means that there is a distingiushment between an EL Variable and a Sciprting Variable - in that case - how are they different? . If we talk in terms of JSP getting converted to a servlet - what would be the scope of these respective variables and in which function(s) would these variables be declared in??. Please throw more light on this.


--------------------------------------------------
Another: How does one declare a variable that can be accessed by core JSTL tags? Two ways I know of is to set the variable in the scope using either

Is there any other way?? without setting the variable in a given scope?

------------------------------------------------

Regarding the usage of scriptlets in the JSTL core lib tags. Why doesn't the below code work?


The below prints a b c d e f g h i j k l even though the delims is given as z,z or if you give it as ",f" - How does it work thus??


-----------------------------------------

Thanks !
Shivani.


/** Code speaks louder than words */
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

I try to answer your questions.

When you define variable using statement



Here the attribute is set in the page scope. It is very much similar to the follwing statement



No Scripting variables are introduced.

When You use useBaen

<jsp:useBean id="bookId" type="java.lang.String"/>

Though the above bean is not useful, as the String Object d nat have any useful gettere and setter properties. It is valid. This actin do the following things.

As there is no scope is defined, and no class attribute is present. The action will search the existing page attribute "bookId". It is previously defined in the page scope by the <c:set> JSTL action, so it use the same.
Otherewise if the class attribute of useBean is defined and the attribute "bookId" is not present in the page scope, It will create new String object which value is "" (Empty String).

Also in the present example new scripting variable ise introduce in the current method that is _jspService(..) with the name bookId, which is similar to

<% String bookId = value assigned by bean action %>

So You can use scripting variable like this

<%= bookId %>

The Following Statement

<% cart:remove(bookId); %>

Let us here assume that the tag remove the bookId from the page scope. Note here is that , though the attribute 'bookId" is removed from the scope, the scripting variable bookId still have reference to the object, so you can use this reference in the scripting element.

Another Look at the Example is


You can see the detail codes of these If you view the generated servlet from the JSP.
---------------------------------------------------------------------------

JSTL variable means the attributes in any one of the scope page,request, session,application.

---------------------------------------------------------------------------

<%c ut value="<%= f>" />

Does not work because there no scriptiong variable f. The JSTL action introduce the attribute named "f" in the loop scope ( I don't know how it is strored) which you can access only in the tag body.

In the last code It is not clear - what is the value of the attribute s1.


Hope this help.


Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Thanks Narendra for your detailed reply.

Just a few more clarifications:

1. What is the difference between scripting and EL Variables

2.

Here s1 = has the values = a,b,c,d,e,f,g,h,i,j,k,l. The below prints a b c d e f g h i j k l even though the delims is given as z,z or if you give it as ",f" - How does it work thus??

Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,


JSTL variable means the attributes in any one of the scope page,request, session,application.

Scripting variable means your normal java local variables, instance variable, static vaiables and so on.

The attributes ( JSTL variables) stroed in the respective scopes. That is page attributes are stored in pagContext, request Attribute Stored in request scope etc.

----------------------------------------------------------------------------

In the example you use delim as 'z,z' that means the delimitares are ',' or 'z'. Note here that delim is single character. Each character in the delim string is treated as seperator, not whole string is treated as delim.


Thanks.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,


The EL variables/JSTL variables are located using he pageContext.findAttribte(..) method. That is when the attribute name encounterd as variable the containe search the value in order page,request,session,application scope.


Also note that the getAttribte and setAttribute standard actions also use the same mechnism if findAttribute, It will not use the scripting variable reference.


Thanks
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Can we summarize this by saying that
c:set and jsp:useBean both - create variables in specified scope but the difference is ...
jsp:useBean creates both Scoped and Local Variable
while
c:set creates ONLY Scoped Variable

Please confirm.

Regards,
Shivani.
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

According to me, you are correct.

Thanks
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Thanks Narendra for confirming this.

Just wanted to add one more point: For those reading...

If jsp:useBean is used in a scriptless page
OR
If jsp:useBean is used in the body of tag which is defined to be scriptless

-->NO local (Scripting) Variables are created.

Regards,
Shivani
[ August 07, 2006: Message edited by: Shivani Chandna ]
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

If you are using scriptless environment, there is no use of scripting variables, so they are not created by usBean action. This is also true for the implicit objects (out, page, pageContext, request, response etc.) created for scripting.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Scripting with JSTL Core tags.