• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we use EL inside scriptlets

 
Arut Jothi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get error when i specify like the following

<%
String x = "${param.name}";
%>

OR

<%
String x = ${param.name};
%>

I saw an example case for Java bean in HeadFirst. So please someone help me out
 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use EL inside scriptlet tags. Everything inside the scriptlet tags must be Java and the expression language is not Java.
 
Arut Jothi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anthony. So we can not use the power of EL. For example,

<%
String s = request.getParameter("name");
String url = "test.jsp';
if (s.equals("jothi"))
url = "test10.jsp";
%>

In the above example I store "name" to a variable and check to see the value. How do I store like above using EL? is there a way to do so?
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arut,

We don't usually set values in a JSP if we follow the MVC pattern. The view is meant for presentation only.

But if you wish to set values into the page, request, session or application, you could do so using JSTL <c:set>. For e.g.,



This way, you should be able to set your value(s) in the JSP & use it for display.

HTH.
 
jack wu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's really works on String,but if you try java.lang.Integer ,oops!

[ September 14, 2004: Message edited by: big moose ]
[ September 14, 2004: Message edited by: big moose ]
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by big moose:
It's really works on String,but if you try java.lang.Integer ,oops!

[ September 14, 2004: Message edited by: big moose ]

[ September 14, 2004: Message edited by: big moose ]


big moose,
Welcome to the Ranch!!!

JavaRanch has not much rules, but a small naming policy. You might wanna cahnge your name here.

Thanks...
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When any value is shown on a JSP page, its all String. You can't tell any difference between an Integer 1 & a String 1.

According to the API for JSTL <c:set>, the attribute type is a String. So you could just invoke the toString() of your Integer object & convert it into a String then.
 
jack wu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I call Integer.toString() without scriptlet?I mean there's only standard actions,jstl and expression language?
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I meant was something like this:

 
jack wu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I think sun don't like us to set values in a JSP with new technology!They even didn't do a little work to support it!
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not really, Jack, the point is for a clean MVC design, JSP being the view, is really just meant for presentation.

It could get really messy when you start to have a lot of business logics or Java codes inside your JSP. The next person who is going to maintain the JSP would have a tough time understanding it.

Also, the webpage designer won't know Java. So by embedding Java into JSP, they too are going to have a tough time figuring out how to make changes.
 
Arut Jothi
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it is a new design and new jsp. well things are ok and easy to follow MVC or etc.,. What about legacy code? When you go for fixing a bug or open existing code, you see logics written there in jsp (based on the capabilities of earlier version of JSP). So If we upgrade to app servers supports JSP2.0, obviously one will try to use EL. In that case, immediatly we can not use it until we refactor the code to MVC model or someother way as mentioned in the above replies. It would have been nice if we are able to use EL code inside scriptlet someway. I think Sun clearly enforces that pure java code should be written inside scriptlet.
 
Chengwei Lee
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Arut,

If you're talking about legacy codes, before JSP 2.0, you won't have EL at all. You either have scriptlets or custom tags.

Although <c:set> takes in String as its attribute type, you could write your own custom tag for setting values, and you could specify the attribute to be of type Integer.

HTH.
 
jack wu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheng is right!The final solution is custom tag,you can do everything with it,and it's a new place for scriptlet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic