JavaServer Pages (JSP)

JavaServer Pages (JSP) is a Java technology that allows software developers to create dynamically-generated web sites, with HTML, XML, or other document types, in response to a Web client request. The technology allows Java code and certain pre-defined actions to be embedded into static content.

A dynamic web page consists of markup language code as well as programming language code. Instead of serving the page as is to the clients, a server processes the programming language code, replaces the code with the data generated by the code and then sends the page to the client. This methodology of embedding programming languages that is embedded within HTML is called the Server Side Include and the programming language that is embedded within the HTML is called the Scripting language.

Therefore JSP pages typically comprise of:

  • Static HTML/XML components.
  • Special JSP tags
  • Optionally, snippets of code written in Java called “scriptlets.”

Consequently, we can create and maintain JSP pages by conventional HTML/XML tools. It is important to note that the JSP specification is a standard extension defined on top of the Servlet API. Thus, it leverages all of our experience with Servlets.


Servlet vs. JSP

Servlets and JavaServer Pages are complementary APIs, both providing a means for generating dynamic web content. JSP doesn’t give us anything that we couldn’t in principle do with a Servlet. A Servlet is actually a Java class implementing the javax.servlet.Servlet interface that runs within a Web container or Servlet container, servicing client requests forwarded to it through the server. In Servlets, HTML tags are embedded within Java codes (generally within out.println statements), whereas in JSP pages, Java codes are embedded within HTML tags. So it is more convenient to write (and to modify) JSP codes than to write Servlet codes. That is why unlike Servlets, which is a programmatic technology requiring significant developer expertise, JSP appeals to a much wider audience. It can be used not only by developers, but also by page designers, who can now play a more direct role in the software development life cycle.  

Another advantage of JSP is the inherent separation of presentation from content facilitated by the technology, due its reliance upon reusable component technologies like the JavaBeans component architecture and Enterprise JavaBeans technology. By separating the look from the content we can put different people on different tasks: our web page design experts can build the HTML, leaving places for our Servlet programmers to insert the dynamic content.

That’s why JSP is preferred over Servlets. The power of JSP is that it is Server-based and provides a framework for Database-driven Web application development. Rather than choosing between Servlets and JavaServer Pages, we will find that most non-trivial applications will want to use a combination of JSP and Servlets. In fact, the JSP and Servlet are based around the concept of the Web application, combining the two APIs into a unified framework.


A JSP becomes a Servlet

A JSP file eventually becomes a full-fledged Servlet running in our web application. This looks like any other Servlet, except that the Servlet class is written for us – by the Container. The Web container translates our JSP file (e.g. test.jsp) into a Servlet class source file (, and then compiles that into a Java Servlet class (test_jsp.class). After that, the container loads the Servlet class, instantiates and initializes it as a Servlet object, makes a separate thread for each client request, and calls the Servlet’s service() method.

Figure 1: Steps to convert a JSP file into a Servlet


The Advantages of JSP

Separation of static content from dynamic content: With Servlets, the logic for generation of the dynamic content is an intrinsic part of the Servlet itself, and is closely tied to the static presentation templates responsible for the user interface. Thus, even minor changes made to the UI typically result in the recompilation of the Servlet. This tight coupling of presentation and content part results in brittle, inflexible applications. However, with JSP, the logic to generate the dynamic content is kept separate from the static presentation templates by encapsulating it within external JavaBeans components. These are then created and used by the JSP page using special tags and scriptlets. When a page designer makes any changes to the presentation template, the JSP page is automatically recompiled and reloaded into the web server by the JSP engine.

Write Once Run Anywhere (WORA): JSP technology brings the “Write Once, Run Anywhere” paradigm to interactive Web pages. JSP pages can be moved easily across platforms, and across web servers, without any changes.

Dynamic content can be served in a variety of formats: There is nothing that mandates the static template data within a JSP page to be of a certain format. Consequently, JSP can service a diverse clientele ranging from conventional browsers using HTML/DHTML, to handheld wireless devices like mobile phones and PDAs using WML, to other B2B applications using XML.

Recommended Web access layer for n-tier architecture: Sun’s JEE Blueprints, which offers guidelines for developing large-scale applications using the enterprise Java APIs, categorically recommends JSP over Servlets for serving dynamic content.

Completely leverages the Servlet API: If we are a Servlet developer, there is very little that we have to “unlearn” to move over to JSP. In fact, Servlet developers are at a distinct advantage because JSP is nothing but a high-level abstraction of Servlets. We can do almost anything that can be done with Servlets using JSP – but more easily.