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 Eval() inJSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Eval() inJSP" Watch "Eval() inJSP" New topic
Author

Eval() inJSP

Krysty Sullivan
Ranch Hand

Joined: Jun 06, 2002
Posts: 48
Is there eval on JSP as there is in ASP and JavaScript? How can i evaluate a string using JSP or JAVA ? The problem is like this:
I use a string (that defines which function must be called) as parameter, i get it using getRequestParameter?
Example:
Content of Tasks.jsp
*********************************
<%
public String getName{} {
System.out.println{"Roldan"};
return "Pass";
}
%>
*********************************
Content of RunMethod.jsp
<%
@include file="tasks.jsp"
%>
<%
-- str = "getName"
String MethodName = getRequestParameter{str};
-- eval{MethodName} will work in ASP, but how
-- will i evaluate the method name in JSP. I
-- can't use the reflect API because my methods
-- are in a JSP page.
%>
Hope you could advise me asap. Thanks!
[ June 06, 2002: Message edited by: cristy ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

(sounds like a very dangerous idea...)
Why can't you use Reflection in a JSP?

Can you give us an idea about what you are trying to do and why? It would help a lot in determining your requirements.
ta,
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"cristy",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Thanks.
Dave
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I knew I saw it somewhere, looks a lot like the article Untangle your servlet code with reflection, except using JSPs instead of specific Servlet code.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Oh, and I really have a problem with allowing external people access to decide what code to call. By getting the method name from the parameters and calling it, you are assuming that people will only call the methods you want.
Can you absolutely guarantee that no one will ever call your servlet except in the exact way you intend? What if they decide to execute doPost instead? It will call doPost, which will call doPost which will call....
Krysty Sullivan
Ranch Hand

Joined: Jun 06, 2002
Posts: 48
Hi Dave, thanks for the quick response.
We have a Tasks.jsp containing jsp methods which will be dynamically called by RunMethod.jsp. No java or class files are involved, pure JSP coding.
e.g.
//Contents of Tasks.jsp
*****************************************
<%!
public String getName{} {
System.out.println("exec getName");
return "Pass";
}
public String getAge{} {
System.out.println("exec getAge");
return "Pass";
}
%>
******************************************
//Contents of RunMethod.jsp
******************************************
<%@ include file="Tasks.jsp"%>
//Tasks.jsp was included so that the methods
//in it would be accessible.
<%
try {
String MethodName = request.getParameter(s);
//s = "getName"
//this is the method name that will be
//accessed in the Tasks.jsp and will be
//evaluated/executed by RunMethod.jsp
*** How are we going to execute any method
*** name contained in the MethodName var,
*** (methods are not in a JAVA file)?
}
catch (Exception e) {
System.out.println(e);
}
%>
******************************************
Please let me know if you still need further explanation. Thanks!!!
Krysty Sullivan
Ranch Hand

Joined: Jun 06, 2002
Posts: 48
I placed the code you provided in the jsp page where i was going to call the eval. It works well.
Thanks David O'Meara!!!
*******************************************
Class jspClass = this.getClass();
String methodName = request.getParameter( "str" );
Method callMethod = jspClass.getDeclaredMethod( methodName, new Class[0] );
callMethod.invoke( jspClass, new Object[0] );
*******************************************
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Happy to help
It's worth re-stating:
Originally posted by David O'Meara:
Oh, and I really have a problem with allowing external people access to decide what code to call. By getting the method name from the parameters and calling it, you are assuming that people will only call the methods you want.
Can you absolutely guarantee that no one will ever call your servlet except in the exact way you intend? What if they decide to execute doPost instead? It will call doPost, which will call doPost which will call....

Have you evaluated the security impact?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Eval() inJSP
 
Similar Threads
string to xpath (nodelist)
bean:write query
JSON problem
Evaluating Strings
Alpha beta pruning with negamax