Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

can we write any java type of code in scriptlets?  RSS feed

 
prasad kakani
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we write any java type of code in scriptlets?


I am using a string object from bean which has been already populated, in the scriptlet in jsp page.That string object contains either "3044" or "-3044".If this object contains -ve string value then one dropdown list box has to be disabled ,if it is +ve value that dropdown list has to be enabled.For this i have written code like this.
<td>
<%
String s=bean.getValue().trim();//it may contain -ve or +ve string value.
if(s.charAt(0)=='-'){%>
<select name="list" disabled>
<option value="s">one</option>
<option value="t">two</option>
</select>
<% } else { %>
<select name="list" >
<option value="s">one</option>
<option value="t">two</option>
</select>
<% } %>
</td>
When i executing this one i am getting can not invoke service method exception error.. and it points errot at 's.charAt(0)' postion....

Exception thrown : java.lang.StringIndexOutOfBoundsException
at java.lang.String.charAt(String.java:385)
at com.ibm._jsp._AdjPType._jspService(_AdjPType.java:245)
at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:88)


Can any one plese help me in this.

Thanks in advance..

[ July 25, 2008: Message edited by: prasad kakani ]
[ July 25, 2008: Message edited by: prasad kakani ]
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by prasad kakani:
can we write any java type of code in scriptlets?

When i executing this one i am getting can not invoke service method exception error.. and it points errot at 's.charAt(0)' postion...



Check your server configuration, or how your project is associated with the server for this exception .

scriptlet is deprecated...but you can use java code inside scriplet
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37234
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasad,
Welcome to JavaRanch!

What happens if s is an empty string? Try checking the string has at least one character before calling s.charAt().

Also note that while you can write Java code in scriptlets, it is considered bad practice. A better idea would be to add isValuePositive() to your bean. Then all the logic could be in Java code rather than a JSP. At that point, you could use JSTL or logic tags to get rid of the remaining scriptlets.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by seetharaman venkatasamy
scriptlet is deprecated...but you can use java code inside scriplet


No scriptlet is not deprecated, only its use is not recommended.



Hope this helps
 
bell saleem
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clearly it is a RunTimeException
the value of s is empty("") string
so is the exception

Also, make it a practice to check for NullPointerException while writing such code.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!