File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Evaluating Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Evaluating Strings" Watch "Evaluating Strings" New topic

Evaluating Strings

Felipe Rocha

Joined: Nov 05, 2001
Posts: 2
Hi there,
Is there eval() on JSP or Servlet as there is in PHP and JScript? How can i evaluate a string using JAVA ? The problem is like this:
I use a string ( that defines which function must be called) as parameter, i get it using getRequestParameter() and after that i want to call the function which has the same name as the string got , how can i do that ?
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
The methods are available: equals(), startsWith(), endsWith(), etc. Check the API.


So much trouble in the world -- Bob Marley
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
One way to do this is to use the reflection APIs (java.lang.reflect), these let you interrogate a class to get info about its members.

I have just started looking at the reflection APIs myself, so there might be better ways of doing this.
sundari swamy

Joined: Oct 19, 2001
Posts: 9

I feel the code given by James is a good start to work on with your requirements.
Few points I noted w.r.t James code , you also have to Prefix the Package name. And I guess the typecasting is also not required .(you have to hardcode the className for typecasting in this case. This wouldn't possibly help you to execute dynamically)
Kris Nelson
Ranch Hand

Joined: Nov 04, 2001
Posts: 35
How many possible values can this parameter have ( i.e. how many functions are there )? If there's not too many, why couldn't you use an if statement with the necessary else if blocks?

Java/XML Web Development
Check it out!<br />Java/XML Web Development<br />Check it out!<br /><a href="" target="_blank" rel="nofollow"></a>
Felipe Rocha

Joined: Nov 05, 2001
Posts: 2
Thanks a lot everybody . I used the reflect API and it worked great.
Thanks again
I agree. Here's the link:
subject: Evaluating Strings
It's not a secret anymore!