I am involved in a jsp/servlet application. The application stores objects needed in session or request. However the same object can be injected into session or request in multiple locations. In other words, instead of writing
HomeBean homeBean = (HomeBean) request.getSession(false).getAttribute("homeBean");
I will write
HomeBean homeBean = BeanStore.getHomeBean();
Am I overthinking or overdesigning ?
In some frameworks like spring or cdi you can inject session or even some session attribute directly to your business component, something like:
Also instead of polluting your code with HTTP session access code just fetch the object once and pass it where needed. It will make reading and testing easier.
Note that your
BeanStore.getHomeBean()is really hard to write correctly if you really don’t want to passHttpSessiondirectly. Servlet filters andThreadLocal, here I come! Not to mention I find such static “factory” quite ugly.Side note, in:
request.getSession(false)will often returnnull, leaving you with nastyNullPointerException. Prefertrueor no parameters at all in this case.