Suggestions for implementing method invocations based on the parameter passed to servlet
I would like to get some suggestions on implementing a solution that invokes ejb methods based on the parameters I pass to a servlet.
I have a web project and an ejb 3.0 project. The ejbs are invoked from the servlet in the web project.
The ejbs are invoked based on the parameters I pass to the servlet. For every action I need to perform, I pass a definite parameter say, task to the servlet. For example, if the task equals fetchEmployee, it will invoke a specific method of a bean for eg:- fetchEmployeeDetails().If the task equals deleteEmployee, it should invoke a different bean method.
I have the following options:
if/else or switch case method. This is becoming messy and unmanageable when the number of tasks increased like anything.
mapping the tasks and the bean classes in an xml config file, and then read it using Digester
Using a ServletFilter to p开发者_开发问答erform some action based on the parameters passed.
Can someone kindly suggest a clean/elegant method to implement this?
You could use dynamic method invocation on your ejb.
You can ask the Class of the interface for a method handle based on the name and parameter signature.
Method method = bean.getClass().getMethod(task,paramTypes);
It gets a bit messy to map the parameters, unless you make your life easy and always pass a hashmap or similar. You'll get only strings from the request anyway so sanitation and translation still must be done anyway.
Then you invoke the method :
Object[] args = new Object[]{paramMap};
String result = method.invoke(method, args);
精彩评论