![]() ![]() ![]() |
|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
JavaServerTM Pages(JSP) technology provides an easy way to create dynamic web pages and simplify the task of building web applications that work with a wide variety of web servers, application servers, browsers and development tools. This article provides an overview of JSP from a developer's perspective, and includes examples of JSP in action. IntroductionJavaServer Pages technology allows web developers and designers to easily develop and maintain dynamic web pages that leverage existing business systems. As part of the JavaTM technology family, JSP enables rapid development of web-based applications that are platform-independent. JSP separates user interfaces from content generation, enabling designers to change the overall page layout without altering the underlying dynamic content. So what exactly is a JavaServer Page? In its basic form, a JSP page is simply an HTML web page that contains additional bits of code that execute application logic to generate dynamic content. This application logic may involve JavaBeansTM, JDBCTM objects, Enterprise Java BeansTM (EJB), and Remote Method Invocation (RMI) objects, all of which can be easily accessed from a JSP page. For example, a JSP page may contain HTML code that displays static text and graphics, as well as a method call to a JDBC object that accesses a database; when the page is displayed in a user's browser, it will contain both the static HTML content and dynamic information retrieved from the database. The separation of user interface and program logic in a JSP page allows for a very convenient delegation of tasks between web content authors and developers. It also allows developers to create flexible code that can easily be updated and reused. Because JSP pages are automatically compiled as needed, web authors can make changes to presentation code without recompiling application logic. This makes JSP a more flexible method of generating dynamic web content than Java servlets, whose functionality JavaServer Pages extend. JSP and ServletsIf you've worked with Java servlets you know that servlets allow you to create dynamically-generated web pages that include data from server-side Java objects. But you also know that the servlet approach to generating web pages is to embed HTML tags and presentation code within a Java class. This means that changes to presentation code requires modification and recompilation of the servlet source file. Because web authors who design HTML pages may not be the same folks as the developers who write servlet code, updating servlet-based web applications can be an involved process. Enter JavaServer Pages, which are an extension of the Servlet API. In fact, JSP pages are compiled into servlets before they are used, so they have all of the benefits of servlets, including access to Java APIs. Because JSP pages are generally presentation code with application logic embedded in them, they can be thought of as "inside-out" servlets. While JSP pages mainly provide a higher-level method of creating servlets, they bring other benefits as well. Even if you're already content writing servlets for web applications, there are plenty advantages to using JSP:
In general, JSP allows developers to easily distribute application functionality to a wide range of page authors. These authors do not have to know the Java programming language or know anything about writing servlet code, so they can concentrate on writing their HTML code while you concentrate on creating your objects and application logic. Creating JSP PagesAt first glance, a JSP page looks similar to an HTML (or XML) page--both contain text encapsulated by tags, which are defined between <angle brackets>. While HTML tags are processed by a user's web browser to display the page, JSP tags are used by the web server to generate dynamic content. These JSP tags can define individual operations, such as making a method call to a JavaBean, or can include blocks of standard Java code (known as scriptlets) that are executed when the page is accessed. To see how this all happens, here's a sample JSP page that includes both static HTML content and dynamic data generated from a JavaBean. When the user accesses this page, it prints the current day of the month and the year, and adds a greeting based on the time of day (either "Good Morning" or "Good Afternoon"). For simplicity, JSP tags are shown in bold:
The above example demonstrates the simplicity of JSP, and also offers a glimpse of the various components of a JSP page. These components include the following:
Using Custom TagsAlthough you can embed Java code within a JSP page to execute server-side processing, JSP also supports an alternative method of inserting dynamic content with custom tags, a mechanism that allows you to invent your own HTML-like tags for your JSP pages. In other words, your JSP pages can generate dynamic content using simple tag syntax instead of Java code. Custom tags are incredibly useful because they provide further separation of responsibilities between developers (who create the custom tags) and page authors (who use them). Creating a custom tag is more complicated than using simple scriptlets in JSP pages, since custom tags require several steps to connect your JSP code to the business logic of your Java components. However, custom tags are also easier to distribute and reuse, and support for custom tags is being implemented in JSP authoring tools. The following example shows a JSP page that generates dynamic content (in this case, today's lunch special) using a custom tag. Notice that in this case we don't need to import Java classes, declare variables, or write any Java code:
The syntax for this page is obviously simpler than the scriptlet example shown the previous section, since it doesn't involve initializing objects and executing their methods. But the JSP page code is only part of the story; for each custom tag, the following three components are present:
We've already seen a JSP page that uses a custom tag, so let's look at the other two components. Tag HandlerA tag handler is a Java class that is
somewhat similar to a servlet. Whereas servlets implement the
which defines the action taken when the
custom tag is processed. If the custom tag includes attributes then
the tag handler must define these attributes and get/set methods for
each. For example, when defining the tag handler for the
Tag Library DescriptorIf you spend all of your time working with Java technology and don't know a thing about XML then the tag library descriptor component of JSP programming may seem strange. Have no fear, you don't need to learn a new language or master a whole new way of programming. Tag library descriptors simply use HTML-like tag syntax to define the name of your custom tag and its attributes, much like defining an object. The following tag library descriptor
defines the
Along with defining the name of an attribute, a tag library descriptor can also define its data type and specify whether the attribute is required; this allows the JSP engine to do certain error checking before the tag handler is executed. Additional information, such as a name and version number for the library, may be included in a tag library descriptor for use with JSP authoring tools. (See the JSP 1.1 specification for complete information on tag library descriptor syntax.) More ExamplesThis section contains additional
examples of JSP in action. In the first example, a JSP page uses the
HTTP request object (
Note that this page accesses the HTTP
request object without declaring or initializing the object. Both the
In this example, static HTML content
that defines page presentation is inserted with ConclusionIf you're looking for a convenient way to create web applications that connect to server-side Java components, JavaServer Pages is the way to go. Besides the inherent portability of Java and JSP's ready access to technologies like EJB, RMI, JDBC, and JavaBeans, the separation of HTML presentation code and application logic makes JSP pages very easy for organizations to work with. In fact, because web authors can create JSP pages with little or no help from you, the Java developer, you may no longer need to worry about creating web pages and writing HTML code.
About the AuthorScott McPherson is the founder of MochaMail Corporation, a Silicon Valley Java technology start-up. MochaMail uses a variety of Java technologies to provide the next generation in web-based email access. Scott can be reached at scottm@mochamail.com. Reader FeedbackTell us what you think of this article.
|