• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Scripting with JSTL Core tags.

 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

According to me, you are correct.

Thanks
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 951
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Space pants. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic