J2EE Design Patterns
Mistake
A single servlet performs duties of model, view, and controller, making the servlet large and difficult to maintain. Watch for It When
Servlets are large and complicated, servlets output HTML directly, or servlets are directly connected to databases or other external resources. Solution
Refactor into the MVC architecture. Separate all output generation into JSP pages or separate servlets. Separate calls to business data into reusable actions. Coordinate input processing, actions, and views with servlets.
Mistake
A single JSP page performs duties of model, view, and controller, making the page large and difficult to maintain. Watch for It When
JSP pages are large and complicated, JSP pages contain a large amount of embedded logic, JSP pages directly connect to databases or other external resources, or JSP pages contain lots of if/then statements. Solution
Refactor into the MVC architecture. Separate all output generation into JSP pages or separate servlets. Separate calls to business data into reusable actions. Coordinate input processing, actions, and views with servlets.
Mistake
Data with too short or long a lifespan is stored in the HTTP session, leading to excessive memory use and potential loss of important data. Watch for It When
Data stored in the session is only used in a single request, data stored in the session is used over a long period of time and should survive web server crashes. Solution
Store short-lived data in request scope. Store long-lived data in the business tier or other persistence mechanism. Make sure that data added to the session is removed when it is no longer relevant. |