Google
 

 

up previous next contents
Up: 3. Day 3: Standard Previous: 3.12 Servlets Next: 3.14 Survey of other   Contents

Subsections

3.13 JavaServer Pages

  • What is a JSP?
  • Why use JSPs?
  • Directives
  • Scriptlets
  • Actions

3.13.1 What is a JSP?

  • JSPs are a combination of HTML code and Java code.
  • JSP files usually have a filename extension of ".jsp".
  • JSP files are compiled into Servlets by the JSP container.
  • Provides automatic session management.
  • Implicit programming objects are available, including request, response, and others.
  • JavaBeans are the component model.

3.13.2 JSP engine/container:

  • Compiles a JSP page into a servlet (once, the first time it is called).
  • Eexecutes the servlets service() method.
  • Sends the resulting text back to the caller.

3.13.3 Translation time and request time

  • Translation time - the time a JSP is compiled into a Servlet.
  • Translation time - certain JSP elements are evaluated at translation time.
  • Request time - the time a JSP is requested by a user.
  • Request time - some JSP elements, such as expressions, are evaluated at request time.

3.13.4 Scriptlets

  • Use <% ... %> syntax to include Java code directly into the JSP.
  • Any valid Java code can be included.

3.13.5 Expressions

  • Use <%= ... %> syntax to include expressions into a JSP.
  • An expression is a piece of Java code that evaluates to some thing or some value.
  • Example: <%= 2+2 %> evaluates to 4.
  • Expressions are evaluated at request time.

3.13.6 Declarations

  • Use <%! ... %> syntax for declarations.
  • Use declarations to declare class scope variables and methods.

3.13.7 Directives

  • Use <%@ ... %> syntax for declarations.
  • Use directives to declare information needed by the JSP engine.
  • Directives include page, include, and taglib.

3.13.7.1 page directive

  • <%@ page contentType="text/html" %>
  • Lets the developer specify packages to include.
  • Lets the developer specify more advanced page features.

3.13.7.2 taglib directive

  • <%@ taglib uri="uriToTaglib" prefix="someID" %>
  • Lets the developer include tag libraries.
  • Tag libraries are generally powerful, reusable components for web developers.
  • Tag libraries can be created by local developers, are available open source, or can be purchased.

3.13.7.3 include directive

  • <%@ include file="filename.jsp" %>
  • Lets one JSP include another JSP or HTML document.
  • The JSP container reads the included file, and creates on servlet.
  • Includes occur at translation time.
  • If you change the included file ...
  • Works differently than <% jsp:include page="filename.jsp" />

3.13.8 Implicit objects

  • As a JSP developer, certain implicit objects are made available to you.
  • The JSP contaner provides them for you.
  • Some objects, such as request and response, are passed as parameters to the servlets service() method.

The table below provides details about each implicit object.

Figure 3.1: List of implicit JSP objects

3.13.9 Exception handling

  • If a JSP causes an exception to be thrown, good practices dictate that the exception is handled by code, or that the user be forwarded to an error page.
  • Exceptions can be caught in scriptlets.
  • Use the <%@ page errorPage="/error.jsp" %> directive to define an error page.
  • Identify the error page with the page directive, <%@ page isErrorPage="true" %>.
  • The JSP container makes the implicit exception object available to a defined error page.