The first thing to understand about
JavaServer Pages is that
it’s a natural extension to the Java Servlet
technology. In fact, after some preprocessing by a translator, JSP
pages end up being nothing more than Java servlets. This is a point
that many beginning developers have a hard time understanding. JSP
pages are text documents that have a .jsp
extension and contain a combination of static HTML and
XML-like tags and
scriptlets. The tags and scriptlets encapsulate the logic that
generates the content for the pages. The .jsp
files are preprocessed and turned into .java
files. At this point, a Java compiler compiles the source and creates
a .class
file that can be executed by a servlet
container.
The translator that turns the .jsp
file into a
.java
file takes care of the tedious work of
creating a Java servlet from the JSP page. Figure 1-2 illustrates how a JSP page is translated and
compiled into a servlet.
JSP technology has become an extremely popular solution for building web applications using the Java platform. JSP offers several advantages over its competitors:
JSP is a specification, not a product. Developers are able to choose a “best of breed” approach.
JSP pages are compiled, not interpreted, which can lead to better performance.
JSP pages support both scripting and access to the full Java language and can be extended through the use of custom tags.
JSP pages share the Write Once, Run Anywhere™ characteristics of Java technology.
As mentioned in the previous section, one of the limitations of using
hardcoded HTML inside of servlets is the problem of separating page
design and application logic programming responsibilities. This
separation is easier with JSP pages, because the HTML designers are
free to create web pages with whatever tools they choose (many of
today’s popular tools are capable of working with
JSP and custom tags). When they are comfortable with the page layout,
the JSP developers can insert JSP scriptlets and custom tags and save
the files with a .jsp
extension.
That’s pretty much all there is to it. When the time
comes to change either the page layout or page logic, the developer
modifies the JSP page as needed and allows it to be recompiled
automatically.
Together, JSP pages and servlets are an attractive alternative to other types of dynamic web programming. Because both are based on the Java language, they offer platform-independence, extensibility into the enterprise, and, most importantly, ease of development.
Get Programming Jakarta Struts now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.