Hi!
I have a design question :)
I want to have multiple servlets but I dont want to configure a new servlet with a new name each time and extend the HttpServlet. I was thinking of having a generic servlet which dispatches different parameters to different specific classes that are actually handling the request.
Ex: theese two calls will be handled by the same generic servlet but dispatched to different services (classes).
/serviceservlet?action=action1¶m1=test1¶m2=test2 /serviceservlet?action=action2¶m21=test
This can be done in many different ways and I dont know for which design to go by. The one I have right now is using guice for bootstrapping and looks like this:
@Singleton public class ServiceServlet extends HttpServlet {
private static final String PARAM_ACTION = "action";
private final Service1 service1; // Service1 is an interface
private final Service2 service2; // Service2 is another interface
@Inject
public ServiceServlet(final Service1 service1) {
this.service1 = service1;
}
@Inject
public ServiceServlet(final Service2 service2) {
this.service2 = service2;
}
@Override
public void doPost(final HttpServletRequest request, final HttpServletResponse response) {
String action = ServletUtils.getStringParameter(request, PARAM_ACTION);
if ("action1".equals(action)) {
... check that the parameters are correct before executing the below to have typesafety
service1.process(request.getParameter("param1"), request.getParameter("param2"));
} else if ("action2".equals(action)) {
... check that the parameters are correct before executing the below to have typesafety
service2.process(request.getParameter("param21"));
}
}
}
In principle I would like to handle the services in a more generic way and I want it to be as easy as possible to add new services later, but I dont want to loose typesafety. What do you think is the best strategy?
/Br joynes