JSPs are an integral part of a web application and so are jsp best practices. If jsps are not coded properly, then it becomes a most unorganized piece of code. You must have seen huge jsps, which end up as a source of defects, and asking you to change them many times. Maintaining such code becomes a very expensive activity. Also it impacts stability of entire application. Following few simple practices can change working on jsp into a happy activity.
1. Reuse Common HTML
There are two main advantages of it. First is – you are maximising reuse and improving productivity. Second is – you are keeping main jsp as small as possible to maintain them better. Small jsp will also help to easily debug. Better way of reusing code is moving the html code to another jsp, and including that jsp at appropriate place. Mostly header and footer information can be common which can be included in each Jsp. You can identify such reusable html code snippets in your jsps, and seperate those out.
Jsp Best Practices
2. Separate Javascript
First good practice would be reducing the java script as much as possible. But under un-avoidable circumstances move it to javascript (js) files and including those in all required jsps. Also, grouping those javascript functions according to functionality is a good idea, this will reduce the amount of javascript you include in each page. Also think of including javascript at the bottom of page instead of top to improve performance.
3. StyleSheets can be Used to Define Themes
This is another component which can be separated out and reused. But there are broader applications of stylesheets. Stylesheets with images and other static contents, contribute to look and feel of a site/all html pages. Now providing theme based styling of site is becoming popular. If you can build some level of theme based stylesheet application some how, then it will be good. Few web tier frameworks (Spring MVC, etc.) offer the feature of theme based look and feel. Depending on other aspects, you can choose one of those. At least you can separate code in stylesheet and include in all jsp pages.
4. Maximise Use of Ready Custom Tags
Select appropriate freely available custom tag library. If possible check if there are any defects in those libraries with respect to the technology set you are selecting. Also see which one provides you maximum number of required features. JSTL tag lib is a mostly used alternative. Also, you can opt for the libraries provided by the selected framework. Struts comes with a rich tag library, which supports many common application features and also advanced features like AJAX.
5. Identify and Build Application Custom Tag Library
This makes life easy of all the developers of application. You put early effort and identify the tag libraries you need in your application. Build those and make available at the time of coding start. This improves productivity considerably and stops all developers from messing up the code in their own style.
6. Use Javabean Based Client Server Data Transfer
Struts, Spring MVC, Tapestry, and JSF provide MVC based frameworks, which enable automatic population of the client submitted data in the defined java beans. This means you don’t need to retrieve all atributes separately from http request in servlet. You can implement your own mechanism for this, if you are not using any framework.
7. Select Appropriate Web-tier Framework
This is clearly a strategic decision to go for a ready framework or not. How your organization permits and how your requirements are satisfied, this will determine which framework you select from Struts, Sprint MVC, Tapestry, JSF, etc. and the supporting frameworks like Velocity, Tiles etc. Identify the benefits according to your needs and select appropriate one. Secondly, also check the support availability for the selected framework.
8. Enable Client Side Caching of Static Contents
Here we come to something related to performance. You may have heavy images and other static contents embedded in your jsp.You may not be changing these everytime the page is loaded. You can enable caching of these items so that the end user gets feel of quick loading of page. Also see how your server’s web container supports this.
9. Define Object Scope Correctly
It is possible that unwanted objects are loitering jsp default objects (session, page, request etc.) un-necessarily, and you get wrong results due to garbage objects. Hence identify scope of objects correctly, and place those in appropriate scope object. This will ensure cleaning of those objects in due time.
10. Identify and Design Special Handling of Browser Buttons
Some times you need to take care of browser buttons separately. It can be refresh button, back button or even browser close button, finalize the requirement and implement it at common place so that all jsp pages can use it.
11. Optimize Amount of Data Getting Displayed
This has to do with identifying and negotiating requirement at right time. If you want the page to be loaded very quickly, then trade off against the amount of data you load on the page. Otherwise, redesiging page after performance testing cycle will be a huge effort.
12. Use Jsp Comment Over HTML Comment
You may not want to send comment data to client. Mostly comments are for developer understanding from maintainability point of view. Hence you don’t want to increase the amount of data getting sent to client side. Hence use jsp comments, which do not get included in the html getting sent to client.
<!– HTML –>
<%– JSP does not go to client to clog network –%>
13. Set Page Caching and Expiry as Required
If you want your page to be loaded everytime a fresh, then set the caching and page expiry meta data directives correctly. Also consider the static data caching point above while doing this.

Share and