• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Evaluating Strings

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The methods are available: equals(), startsWith(), endsWith(), etc. Check the API.

Bosun
 
Ranch Hand
Posts: 403
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Felipe Rocha
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot everybody . I used the reflect API and it worked great.
Thanks again
reply
    Bookmark Topic Watch Topic
  • New Topic