jsp performance hints and tips

6
JSP Performance Hints and Tips Mohan Bang

Upload: mohan-bang

Post on 26-May-2015

2.938 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Jsp performance hints and tips

JSP Performance Hints and Tips

Mohan Bang

Page 2: Jsp performance hints and tips

Caching Hints for better performance Use jspInit() method as cache Here you are generating both static data and dynamic data from _jspService()

method. Instead what you can do is

<%!      public void jspInit(){

//create all the static data here// do same for navbar if it's data is static// do same for footer if it's data is static

} // end jspInit() method%>

out.print(header);out.print(navbar);                    // write dynamic data hereout.print(footer);}

Page 3: Jsp performance hints and tips

Optimization Techniques in Service Use StringBuffer rather than using + operator when you

concatenate multiple strings Use print() method instead of println() method  of out

(implicit) object        Use ServletOutputStream instead of JSPWriter Initialize the out with proper size in the page directive Flush the data partly Minimize the amount of code in the synchronized block Set the content length

Page 4: Jsp performance hints and tips

Optimization Techniques in Page Directive Page directive defines attributes that apply to an entire JSP

page. Here is an example of page directive.<%@ page session="true|false" buffer="none|8kb|size in kb" %>

By default JSP Engine creates session object. If you don't want to use built in HttpSession for a JSP, then make session attribute value as false. It avoids unnecessary creation of session (implicit) object, reduces overhead on memory and garbage collector and increases performance.

By default out (implicit object of JSPWriter) object size is 8kb. You can increase the size if you are sending large data that increases performance.

<%@ page session="false" buffer="12kb" %> here you need to set the size as per page response data if it

crosses 8kb.

Page 5: Jsp performance hints and tips

Choosing the right Session MechanismSession mechanism Performance Description

session goodThere is no limit on

size of keeping session data

Hidden fields moderateThere is no limit on

size of passing session data

Cookies moderateThere is a limit for

cookie size

URL rewriting moderateThere is a limit for

URL rewriting

Persistent mechanism moderate to poorThere is no limit of

keeping session data

Page 6: Jsp performance hints and tips

General Hints

Flush the data partly. Use include directive instead of include action when you want to

include the child page content in the translation phase. Avoid giving unnecessary scope in the 'useBean' action. Do not use custom tags if you do not have reusability. Use application server caching facility. Use Mixed session mechanisms such as 'session' with hidden

fields. Remove 'session' objects explicitly in your program whenever

you finish the task. Reduce session time out value as much as possible. Use 'transient' variables to reduce serialization overhead if your

session tracking mechanism uses serialization process.