JSP ( Java Server Page) Best Practices

Connect with

Oracle Java
There are so many questions arise in mind when we ask yourself best practices of JSP ( Java Server Page). Conceptually, a JSP page is converted to a servlet before it’s called on to serve requests. This means that a JSP page can do anything a servlet can do, but it doesn’t mean JSP is appropriate for all tasks.

Favor Using JSP as View Only

In MVC (Model View Controller) architecture, JSP plays a role of View, and never choose jsp as controller in MVC, although you can use JSP as controller too.

Use as Presentation

Use as a presentation not for business, no business logic in side JSP. Only presentation logic, e.g. displaying of date is the presentation logic so use JSTL or any another tag library on JSP to display proper format as per.

Favour JSTL and Avoid Using Scripting

Avoiding scripting elements (raw Java code in JSP) is always a good idea. Using scripting elements exposes you to, at least, the following problems:

  • Syntax errors are very easy to make and extremely hard to locate
  • Scripting code can be reused only through copy/paste.
  • For non-programmer changed of code for look-and-feel aspects of the pages, scripting code is very distracting and easy to corrupt by mistake.

Break common pages

For consistent look and feel , break the page into multiple pages as per your design, i.e. header , footer, menu, left navigation should be in separate file so that , it can be include efficiently.

choosing Appropriate Include

JSP provides two ways to include content in a page: the include directive (<%@ include file="" %>) and the include action (">

Enable URL Rewriting

To handle cookie-less session tracking, web containers provide URL rewriting as a backup mechanism. URL rewriting works by embedding the session ID in all URLs in the generated response, ensuring that the session ID is returned to the container when the user clicks a link or submits a form embedded in the response.
It’s easy to do with the JSTL action:


Use Common Error Pages for the Application

Write a errorHander.jsp like page to handle common error page and entry error-page element in web.xml.

entry in web.xml



Connect with

3 thoughts on “JSP ( Java Server Page) Best Practices

Leave a Reply

Your email address will not be published. Required fields are marked *