File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Evaluating Strings" Watch "Evaluating Strings" New topic
Author

Evaluating Strings

Felipe Rocha
Greenhorn

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 ?
Thanks
Bosun Bello
Ranch Hand

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

Bosun


Bosun (SCJP, SCWCD)
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.
eg.

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

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?

------------------
WebNelly.com
Java/XML Web Development
Check it out!
http://www.webnelly.com


WebNelly.com<br />Java/XML Web Development<br />Check it out!<br /><a href="http://www.webnelly.com" target="_blank" rel="nofollow">http://www.webnelly.com</a>
Felipe Rocha
Greenhorn

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: http://aspose.com/file-tools
 
subject: Evaluating Strings
 
Similar Threads
How to convert string to IPath
Assigned Value is Changing
String replace function with single quote
A JavaBean inside a JSP function
Dynamic Expression Language Evaluation