This article explains the best practices using Java Server Pages. Explains proper use of scriptlets, beans, custom tag libraries and more...
The following is a list of best practices that you can keep in mind when working with JSP pages:
- Minimize the use of scriptlets.
- Best place for business logic is JavaBeans.
- Try to use existing custom tag libraries.
- Using custom actions.
- Template as much as possible.
- Use the correct inclusion mechanism.
- Use common configuration settings when possible.
- Be user-friendly, and use the JSP exception mechanism.
- Liberal use of JSP comments to make the page author-friendly.
- Make your pages readable.
Minimize the use of Scriptlets:
Use of scriptlets will make you do much more work than actually you plan to do when scripting. You'll find that as the your application grows more complex and more team members become involved, it's much harder to maintain and make it worse for reading. It doesn't matter how hard you try to make-up for readability you end up with planting confusions in your teammates. Main advantage of not using frequent Java code embedded in the JSP page is that finally it'll become possible for any layman in terms of Java, to maintain the presentation page. This, after all, is the intent of JSP. This will let the Java developers focus on business logic as they implement the behavior behind the custom tags and allow the presentation page authors to use the custom tags just as they use ordinary HTML tags. [ top ]
Best place for business logic is JavaBeans:
By putting any business logic within a JavaBean, common behavior not only can be used by other JSP pages but also by other portions of the application. For all intents and purposes, a JavaBean is just a Java class that follows a couple of special rules. They're also easily accessible from any JSP page. This is the best practice for eliminating redundancy. [ top ]
Try to use existing custom tag libraries:
It's always recommended checking out the available custom tags before deciding to write your own. It'll save you time coding and debugging, and it'll allow you to spend more time focusing on your business application. Valuable actions are available within the JSTL, which is the only tag library that's optimized by container vendors. There are also custom tag libraries available with the Struts Framework, as well as on the Jakarta Taglibs project located at http://jakarta.apache.org/taglibs/index.html. [ top ]
Using Custom Actions:
JSP 2.0 has introduced a number of new features such as tag files and simple tag handlers that make it easy and compelling to create your own custom actions. If you have logic that's specific to your application, sometimes it's necessary to write your own custom actions.
Keep in mind that when writing custom actions they're reusable within JSP files, but not necessary outside of the JSP pages. This is where you need to determine if it makes sense to create a custom action or a JavaBean that can potentially be reused in different areas of your model. A compromise is to try and extract common behaviors or business logic. Utilize JavaBeans or EJBs that perform those common behaviors and call them from the custom action. That way you can take advantage of the best of both worlds. [ top ]
Template As Much As Possible:
We use template mechanism when building large web sites that have a common look and feel. This allows for a common file to control the layout of the application. When it's time to make changes to the layout, you'll have to modify only the one control file. The rest of the pages will automatically reflect the layout change. This is a much quicker way to make changes in your Web application and to enhance the maintainability of your code. A number of template custom tag libraries are specifically for this purpose and are included with such frameworks as Struts. Use of stylesheets also allows for a single file to dictate the appearance of your pages. For example, Cascading Style Sheets (CSS) are commonly used to control such display characteristics as fonts, font sizes, and table layouts. As with templates, stylesheets allow for a one-stop shop for making display control changes. These changes will then be picked up by all of the JSP pages at once and increase the maintainability of your code. [ top ]
Use the correct inclusion mechanism:
It is always better to make changes in one place rather than having to do it in multiple places. You can use JSP include mechanisms. If the content changes frequently, use the include action. If the file is primarily static (HTML or template text), use the include directive.[ top ]
Use Common Configuration Settings when possible:
Its recommended to combine common configuration settings into one JSP file and then include that file in each of your JSP pages. It provides a single point of contact for all of the custom tag library prefixes. If a team is working on different pages it's much more consistent to just define all custom tag libraries in one file and then have everyone access the same prefixes throughout the application. [ top ]
Be User-Friendly, and Use the JSP Exception Mechanism:
Make use of the JSP error page mechanism. When using an error page, it's possible to give an user-friendlier message and point the user in a direction that will allow them to continue with their work or contact the appropriate person. You can use the error page to log any information that would be useful for the developer without the unsightly exception stack being displayed to the user.[ top ]
Liberal use of JSP comments to make the page author-friendly:
Comment within JSP code so that it's easy to follow. HTML comments, which are in the form , will be viewable in the compiled JSP page's HTML source code. This means that any user can go to a View Source from their favorite browser and view your comments, which may not be what you want. Using JSP comments in the form <%-- JSP comment --%> means they can't be viewed as part of the page's source through the browser. Java comments can also occur in JSP pages inside Java scriptlet sections. Most of the time use JSP comments.[ top ]
Make Your Pages Readable:
Make sure you include line spacing between logical sections. Comments preceding the JSP code can also help explain what the reader is about to see. Make sure you close your tags.[ top ]
|