Lets look at some tips to optimize the j2ee Application
1.Do not store large object in javax.servlet.http.HttpSession.
There are two reasons for this
– Servlets may need to serialize and deserialize HttpSession objects for persistent sessions, and making them large produces a large serialization overhead.
– It will consume more memory.
2. For Web pages that don’t require session tracking, save resources by turning off automatic session creation using: <%@
3.Do not use javax.servlet.SingleThreadModel unless it is really required.
4. Minimize use of System.out.println.
5. Reuse EJB homes
6. Call HttpSession.invalidate() to clean up a session when you no longer need to use it.
7. Use the include directive where possible, as this is a compile-time directive.include action is a runtime directive. So the static pages can be included using
8.Use JNDI caching with synchronization Example: JNDI caching
9. Use a ServletOutputStream rather than a PrintWriter to send binary data.
10.Initialize the PrintWriter with the optimal size for pages you write.
11.Minimize the synchronized block in the code.
12.Remove HttpSession objects explicitly in your program whenever you finish the session;
Example : On click of sign out button
13. Set the session time-out value as low as possible
14. Use transient to avoid sending data that doesn’t need to be serialized.
15. Use StringBuffer/StringBuilder rather than using + operator when you concatenate multiple strings.
16. Custom tags leads to performance overhead.So use as few custom tags as possible. Keep performance in mind while designing your custom tag library.
17. Where ever possible, use RequestDispatcher.forward() instead of HttpServletResponse.sendRedirect(), as this involves a trip to the browser.
Database level optimization
1. Use PreparedStatements rather than plain Statement objects.
2. Use database stored procedures.
3. Do batch updates to reduce database calls.
4. Tune the connection pool size to minimize the creation and destruction of database connections.
5. Keep the transactions as short as possible. The longer a transaction is open, the longer it holds the locks on the resources acquired, and every other transaction must wait.
6. Do not use DISTINCT clauses unnecessarily. If you know the rows will be unique, do not add DISTINCT in the SELECT clause.
7. Avoid using SELECT *.Instead select only the columns from which you need the data.
8. Add indexes. Indexes can result in a big performance gain, as shown above, even though they consume extra disk space
9. Avoid using too many string functions and operators in the query. String functions like SUBSTRING, LOWER, UPPER, and LIKE result in poor performance.