The web-app I'm currently building follows MVC. I've structured the servlets in a way such that they each handle related actions (so, one servlet may be responsible for all actions relating to setting up a user's account, while another may be used for user correspondance). Currently I have an "action" parameter attached to all query strings (for GETs), and request bodies (for POSTS), and use that in order to determine what action to take in these "related action" servlets.
In other words, my code is structured somewhat like this:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
String action = request.getParameter("action");
if(action.equals("login"))
{
.....
}
else if(action.equals("createAccount"))
{
....
}
else if(action.equals("changePassword"))
{
....
}
..........
}
I'm relatively early in the development process, and as of now, I don't find any maintainability issues coming from this design. As per MVC, all the servlets do is call the methods of my utility classes; no actual work is done in them except for sending the responses back to the client. But by browsing SO, I'm coming acros开发者_StackOverflow社区s the sentiment that even moderately-sized (~10 or less "elses") if-else statements are code-smell and suggest the user isn't doing things the "OOP way".
Is there a better way to filter the types of requests coming in to my servlets? I'd like to be able to improve the maintainability of my app, but unwilling to implement OOP features just because they're OOP (similar to how people despise purists who insist on rigid encapsulation, to the point where they have 24 one-line getters/setters).
Have you considered using a pre-baked MVC framework like Apache Struts, Spring, or Google SiteBricks? All of these are great, and might significantly reduce your code burden. I'd strongly recommend going that route. You'll save yourself some headaches and maintenance nightmares over rolling your own.
If you can't, for some reason, then honestly I don't think there's necessarily a problem with what you've done. It seems clear and maintainable. In effect, what you've done, is very similar to the way that micro-frameworks like Sinatra work for basic URL handling, albeit they do so with some nice syntactic sugar.
Outside of using a framework as Jonathan suggested, you could also break functionality into multiple servlets. You would have to decide where the logical break is but your URLs would then resemble something closer to a REST API.
An example URL would be http://myapp.com/create_account or http://myapp.com/login
Each of the URLs could map to separate servlets.
If you're just looking to avoid code-smell with the the large if statements and be a bit more OO, you can try using reflection-style instantiation if you can get your utility classes to implement the same interface. Basically, the value of the action parameter would be a representation of an actual classname (or a portion thereof, with appropriate values prepended or appended).
String action = request.getParameter("action");
Class clazz = Class.forName(action);
UtilityInterface utilityObj = (UtilityInterface)clazz.newInstance();
精彩评论