aspose file tools*
The moose likes JSP and the fly likes Accessing a variable declared in JSTL in jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Accessing a variable declared in JSTL in jsp" Watch "Accessing a variable declared in JSTL in jsp" New topic
Author

Accessing a variable declared in JSTL in jsp

Nandhini Sridharan
Ranch Hand

Joined: Oct 26, 2006
Posts: 36
I have a variable defined in JSTL using c:set tag. I would like to use the value of that variable within a scriptlet in the same jsp. Can anyone help me out on this.

Thank you.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
What are you trying to do? Most of us would prefer to help you figure out how to do it without using scriptlets.
Nandhini Sridharan
Ranch Hand

Joined: Oct 26, 2006
Posts: 36
I am actually instantiating a class within scriptlet. I am passing a value of a variable to that class's constructor in order to get it instantiated with those values. The variable is declared in jstl tag and the class instantiation is happening within scriptlet.

Regards,
Geetha.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Again, why?

Mixing JSTL and scriptlets is rather messy and not recommended. Why aren't you using JSP actions or custom tags?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Stefan Evans
Bartender

Joined: Jul 06, 2005
Posts: 1018
Understanding how things work never hurts in my opinion.

The JSTL variable space is the attributes in page, request, session and application scopes.

so if you had <c:set var="meaningOfLife" value="42"/>, that sets the pageContext attribute "meningOfLife" to the string "42".
You could get it in scriptlet code as pageContext.getAttribute("meaningOfLife")

Having said that, I am fully in agreement with the other posters that JSTL and scriptlet code don't mix.
The class should either be instantiated in a servlet, and set into an request attribute before you forward to a jsp, or some other method of population considered.

Does it really need to be in the constructor?



You could then have the following


Ok its a hack of a workaround, but it avoids use of scriptlets.
It just means you use the parameterless constructor, and then call a setter method, as opposed to passing the value in a constructor.

Cheers,
evnafets
Nandhini Sridharan
Ranch Hand

Joined: Oct 26, 2006
Posts: 36
Hi,

Thanks for all your help. I am very very new to JSTL environment. So I will give you sample code.

In the above code, for the value of <VALUE OF PART NUMBER>, I can either take it from request object. If the request object doesnt contain value we have to take it from variable(jstl variable using c:set) defined in that jsp.

I want to know how I can perform the above task using JSTL tags?


[BPSouther: Added UBB Code Tags]
[ December 07, 2006: Message edited by: Ben Souther ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

JSTL was not meant to be a fully functional programming language to replace scriptlets.

The intent was to give a JSP developer enough tools to work data structures (passed to the JSP via scoped variables) to be able to format and mark up the data as HTML without needing to resort to scriptlets.

When writing scriptless JSPs, you will want to employ MVC techniques to insure that all the heavy lifting is factored out of your JSPs into Servlets, Beans, and Plain Old Java Objects (POJOs) before context is passed to the JSP.

In my humble opinion, the only thing worse than a JSP with scriptets in it is a page with both scriptlets AND JSTL/EL code.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Nandhini Sridharan
Ranch Hand

Joined: Oct 26, 2006
Posts: 36
Hi,

Please tell me how to implement the above code only using jstl and not using scriptlets within jsp. As using scriptlets and jstl within a jsp is not a good practice.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

The code cannot be converted as-is because it contains calls to methods that require parameters. This is not compatible with the EL.

Using JSP 2.0 and JSTL/EL requires a slightly different way of thinking about the data that is sent to a page. Rather than sending whatever random structure you have available and trying to contort the page code (as you are trying to do) to adapt to it, your controller should do all the hard work and create data structures to send to the page that are easy for the page to handle with straight-forward JSTL and EL.

This article discusses this concept.
[ December 07, 2006: Message edited by: Bear Bibeault ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Nandhini Sridharan:
Hi,

Please tell me how to implement the above code only using jstl and not using scriptlets within jsp. As using scriptlets and jstl within a jsp is not a good practice.


I think you missed my earlier point.
JSTL is not a general purpose language and is not capable of replacing what you've got in your scriptlets. To implement a proper scriptless page, you will need to factor that scriptlet code out of your JSP altogether.
Nandhini Sridharan
Ranch Hand

Joined: Oct 26, 2006
Posts: 36
Hi,

Thanks for the reply. I can understand now. I am planning to write jspf for that part of logic alone and include jspf in the main jsp. I hope that would do.

Thank you so much for all the help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61010
    
  65

Sweeping the dirt under the carpet is rarely beneficial.
Nandhini Sridharan
Ranch Hand

Joined: Oct 26, 2006
Posts: 36
Thats true. But I think I dont have any other option here.
Jeroen T Wenting
Ranch Hand

Joined: Apr 21, 2006
Posts: 1847
You do. Rethink your architecture to a proper one where the business logic is NOT performed inside the JSP but in a servlet that uses the JSP only for presentation purposes.


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing a variable declared in JSTL in jsp