views:

229

answers:

6

I make static classes in servlet, these classes are controllers for web pages. Requests are redirected to controllers by controller urls that are in static HashMap that contains all controllers. Are there any posibilities that user/session realted stuff could get messsed up with other users sessions? I will of course save all session related data to HttpSession and request, response and HttpSession objects are given to controller when controller request handler is called.

UPDATE:

Here is example:

public class MainControllerServlet extends HttpServlet {
static HashMap<String, ControllerAbstract> controllers = new HashMap<String, ControllerAbstract>();

    public MainControllerServlet()
    {
        controllers.put("url", new Controller());
    }

}

ControllerAbstract:

public abstract class ControllerAbstract {
    private String url;
    private HashMap<String, ControllerAbstract> children = new HashMap<String, ControllerAbstract>();

    public abstract void handleRequest(HttpServletRequest request, HttpServletResponse response, SessionBean session, String type) throws Exception;

}
+1  A: 

As far as I can recall a static class is nothing more than a nested class. So I don't see why it should be different than a normal public class.

NickDK
+2  A: 

You mean Static references? there is no such thing like static classes, unless they are inner classes, but they dont have static behavior. If you are using static references as controllers, you have care about all atributes in these controllers.

marcos
You seem to already know it, but writing "there is no such thing like static classes" is a little bit unfortunate...
Tim Büthe
+1  A: 

Static inner classes behave exactly like normal classes, they just have an additional scope. That said, if you are sharing state between the classes by using static variables in the outer class then this could lead to problems. Given that you have said that you will only be using the HttpSession object to pass data between invocations then this will not be a problem for you.

Paul Wagland
+1  A: 

It looks from your code like you're using a static hashmap, not static classes, so you're sharing the map between all users. There are a few problems I see with this-

  • Your map is not thread safe, which could be an issue with conflicts between object creation and retrieval. Given the potential sharing/threading issues, where else are you putting objects into the map?
  • Your ControllerAbstract objects have state, and you can run into a situation where multiple requests will be using the same controller. For example, will any requests add to the children hashmap in their own ControllerAbstract objects? You could have multiple requests using the same ControllerAbstract - e.g. if request 1 wants to add or delete to it's ControllerAbstract.children map while request 2 referencing the same object decides it needs to iterate through the children you'll get a ConcurrentModificationException.
  • Servlet construction is managed by the container. If you're going to have to hardcode the url to have it exist in the servlet's constructor, is it just a constant string? If so, wouldn't it be easier to just declare it as a constant in the ControllerAbstract object? If not, where's it coming from? In any case what your doing is altering the map of controllers whenever the container decides it needs to create a new servlet.
Steve B.
What could be thread safe choice for controller storage? or should I call controllers by Url's ? for example /site_url/controller_url/ => controller_urlController (Class name) and create a new class by class name ?
newbie
+1  A: 

Static inner classes are an alternative to normal classes in another package. So you won't have to worry about them getting mixed up with your servlet classes.

Because you call the .handleRequest() methods as a result of servlet calls, you must make sure that the implementation of your controllers is re-entrant. I.e. their execution should not use variable storage other than the stack and the objects you pass as arguments.

That said, I would opt to put the controllers in their own package outside of the package that contains your servlets. You can then add a manager class that owns and initialises the map and handles calls from servlets (abstracting the implementation details.)

rsp
All controllers are created in servlet constructor and there will be no updating or modifications to those controllers later
newbie
+1  A: 

What you're doing looks ok to me. Just make sure you don't store anything in member variables in your Controller implementations. I'm also assuming you are only putting objects into the controllers map at servlet instantiating time.

delux247