Here are few coding tips to increase the performance of your jsp page
  • Jsp page consists of both static and dynamic datas, as name refers static data remains constant through that jsp page while dynamic data change considering the nature of that particular jsp page.Hence it is not required to recreate static data each time the page is requested instead we write those in jspInit() method which only calls once in the life time of that particular jsp page.
  • Use of string literals instead of string objects since literals are immutable where as objects are mutable.Also use the + operator to concatenate strings over StringBuffer if all the strings are known prior to compile time and use StringBuffer to append strings for concatenation if they are known only at the run time.StringBuffer with indication of length of string adds on performance.
  • Use print method instead of println method to generate page so as to reduce a small overhead as println internally calls the same print method.
  • Use of Servlet Output Stream to send binary data is more preferable to JspWriter since latter is used actually to send character stream
  • Incase of passing large amount of content from servlet to client, flush() method can be used to partially flush out the data so user does not have to wait for the whole content. He can see the content splits one by one.
  • Synchronized methods comes handy when you need order processing of data .Use this method when its need is absolute and also make sure that the code inside this method is minimal.
  • If session values are not required in a particular jsp page of your application just make session attribute of the page directive false so that unnecessary creation of session objects and garbage collection overhead is reduced. Increase the buffer attribute size which by default is 8kb if you have larger response content.
  • There are two type of include mechanism 1).include directive eg: <%@ include file=”top.jsp” %> and 2).include action eg: , Former includes the top.jsp page at the time of translation while latter includes the page at the time of run time.Hence the former does not create any overhead while latter does. Include action can be used in page when include directive fails, i.e when code exceeds maximum line of compilation and page does not compile.Since the jsp page included using “include action ” is complied separately.
  • Using the scope attribute in useBean action wisely. The scope actually refers to the life time of the object created. There are four scopes namely page,request,session and application.Setting scope as session and application where it is not needed imposes overhead on memory.
  • Jsp provides custom tags as an alternative to write java code in jsp page this increases readability,reusability and simplifies the code but avoid these custom tags unless it is frequently used in your applications since good amount of time is consumed over identifying and executing the tag handlers.
  • Caching of datas always improves the performance by reducing the CPU cycles.You can cache datas by using jspInit() as mentioned above,method to cache static data,write algorithms to cache dynamic data,Some server provides caching for dynamic content,using of session and application and third party algorithms for effective caching.
  • Session mechanism are used to maintain client state across number of pages they are Session,Hidden fields,Cookies ,Url rewriting and Persistent mechanism.Choose this according to your needs.
  • Remove the session explicitly after the requirement usually server provides session time out to remove the session objects after a period of time which can be set in the server this process imposes memory and garbage collection overhead also in this case after exceeding certain memory limit session objects may be serialized by server avoid these by explicitly remove session via session.invalidate() method.