• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scriptlet and Expression!!!  RSS feed

 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello all,
could you please help me on understanding the difference between Expression and scriptlet? why and where each one of them is used?
thanks.
 
Atul Samnerkar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Expression will give the resultant value. on the other hand scroptlet will form the part of the servlet. i.e. member variable or member methods.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write java code (for loop, declarations)in scriptlet but expression just allows evaluation of expression(for e.g. "Hi" + "Namasthe", bean.getValue())
 
Steve Winer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be more complete, here is an example of both:
<tr>
<% for(int i=0;i<10;i++){%>
<td>Column <%= i %></td>
<% } %>
</tr>
All scriptlets start with "<%" and end with "%>". They can contain any Java code and have access to the JSP variables request, session, page, etc. They can any amount of lines. You can create local variables and do just about anything you want in them.
Expressions start with "<%=" and end with "%>". They can not contain code blocks, just one line that evaluates to something. They can reference variables that have been declared local or variables that have been defined in declarations. Either way, they can only reference variables that have been declared in JSP code that appears above the expression. The variable accessed or expression that is present is evaluated and the resulting value is substituted in line to the JSP.
Steve
 
Brian Smith
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the replay folks. i really appreciate it. now i am still trying to find out a scenario where expression is the only solution and scriptlet can't be used. any idea?
thanks.
 
Pradeep bhatt
Ranch Hand
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For e.g.
welcome <%= name%>
 
Sue Spielman
Author
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The EL also has a number of advantages over Scriptlets. Such as
[LIST]
[*]cleaner syntax
[*]implict objects
[*]ability to access data structures
[*]operators
[*]automatic type conversion
just to name a few. Besides which, if you are using scriplets in your JSP code you are probably performing some type of logic that is better served in an Object.
Sue
 
vasu maj
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These are two different things altogether. Although you can use one in place of the other in most cases using an expression is convenient wherever you can use it as you can see from the examples given above. Scriptlets are where you write java code as you would write in a normal java application and surround it with scriptlet tags.
[jsp:scriptlet] java code [/jsp:scriptlet]

Expresions can be used in your HTML to print the value stored in the variable by using the expression tags.
This is how a [jsp:expression]variable[/jsp:expression] is printed using expressions.
Vasu
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!