We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

illegal start of expression

royalcyber
royalcyber asked
on
Medium Priority
963 Views
Last Modified: 2008-02-07
I have  a method in JSP which checks if the value is null if it is null it returns ""

here is the code

<%
public string convert_null(string value){
   if (value==null || value.length()==0 || "null".equals(value)){
      return "";
   } else {
      return value;
   }
}
%>

I get this error

public string convert_null(string value){
illegal start of expression

Any help will be greatly appreciated

Thanks




Comment
Watch Question

student
CERTIFIED EXPERT
Commented:
Try it this way.

<%!
public string convert_null(string value){
   if (value==null || value.length()==0 || "null".equals(value)){
      return "";
   } else {
      return value;
   }
}
%>

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
rrzstudent
CERTIFIED EXPERT

Commented:
You tried to put your method into scriptlet. Code in scriptlets is put inside the   _jspService method. To see this look in Tomcat's work folder.
CERTIFIED EXPERT
Top Expert 2006

Commented:
As rrz has correctly pointed out, you need to use a declaration "<%! %>" instead of a scriptlet "<% %>" as shown here http://www.jsptut.com/Declarations.jsp. It is importatne that you understand the diference between a declaration, and expression and a scriptlet.

No points please, this is just a comment.

Author

Commented:
Thanks you very much guys.

Also I would need to use this method in many jsp's. Now is there a way I can put this code somewhere and then try to reuse
CERTIFIED EXPERT
Top Expert 2006

Commented:
Either in a javabean or in another java class that you invoke in a scriptlet..

Author

Commented:
thanks
rrzstudent
CERTIFIED EXPERT

Commented:
Thanks for the points.
I would have been glad to share points with bloodredsun.
Are you using Tomcat ?  Did you find your translated file in Tomcat's work directory ?  You will learn a lot by looking at it. Its name will similar to the name you gave your JSP.   Look for something like   yourName_jsp.java           rrz
CERTIFIED EXPERT
Top Expert 2006

Commented:
Thanks rrz but these points are all yours ;-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.