JavaServer Pages (JSP)


JavaServer Pages are a template-based approach for creating dynamic Web pages where Java code is embedded within an HTML page. The premise behind using JSPs is to enable the Java code within a Web page, which is used for business logic, to be separated from the HTML/XML presentation instructions. This approach permits a Web page designer to focus on designing the Web pages with very little understanding of the Java programming language, and enables the Web developer to focus on the business logic of the page without learning how to design Web pages, which in itself is an art.

Note

JSPs do not require any client-side Java support, such as Java plug-ins or the Java Runtime Environment, as the JSP execution occurs in the Web container, as in the case of applets and Java client applications.


JavaServer Page Request Processing

When a Web browser requests a JSP, the Web container checks if the same JSP has been already been processed . If this is the first time the container has seen the JSP, it is translated into Java servlet code and compiled into a Java Servlet class. The JSP then executes as a Java servlet.

Figure 5.9 depicts the steps the Web container takes to process HTTP requests and provide responses to a calling client.

Figure 5.9. A Web container accepting HTTP requests and providing responses.

graphics/05fig09.gif

Referencing Figure 5.9:

  • A client application sends an HTTP request to the Web container/server.

  • If the request is for a static page, the Web container reads the page off the disk system and posts it back to the requesting client.

  • If the request is for a JSP file (.jsp), the Web container passes the HTTP request off to the JSP engine. If necessary, the JSP engine compiles the JSP file into a Java Servlet. Control is then passed to the servlet execution engine.

  • If the request is for a servlet, the Web container passes the request to the Servlet engine. If necessary, the Servlet engine loads the Servlet class. It then executes the servlet.

  • The servlet then prepares an HTTPResponse object, which the Web container uses to post results back to the requesting client.

The JSP Language Syntax

Because JSPs are transformed into Java servlets, they need language statements to interact with the underlying Java classes. As stated earlier, JSP files are composed of HTML content and JSP-specific XML-like elements. JSPs have three types of elements for controlling the JSP servlet: directives, script control statements, and actions:

  • Directive elements provide environmental controls, such as referencing Java classes, tag libraries ( taglibs ), controlling output buffer sizes, and statically including other JSP files such as headers or trailers . A few directive elements are discussed in the following bullets:

    Note

    Tag libraries (taglibs) are HTML/XML-like special constructs that enable the Web page designer to execute Java code without using Java. The taglib elements map to Java methods that perform operations.


    • <%@include ... %> are begin and end tags that statically include JSP and HTML files of other Web components .

    • <%@page and ... %> are begin and end tags to control page environment features, such as error pages, output buffer sizes, content type, and thread access controls.

    • <%@taglib and ... %> are begin and end tags used to reference tag libraries.

  • Scripting elements allow Java code, methods, variable declarations, and expression output to be added to the generated JSP servlet. There are three scripting elements:

    • <% ... %> are begin and end tags that allow Java code to be inserted between these elements.

    • <%! ... %> are begin and end tags that allow method and data declarations to be made in the JSP file.

    • <%= ... %> are begin and end tags that allow expressions to be evaluated and the results added to the servlet output buffer. The output is converted to a Java String.

  • Action elements allow the JSP Web developer to access Java beans, include dynamic output from other JSPs, and forward requests to other JSP pages or servlets. A few of the action elements are described in the following list:

    • The <jsp:forward ... /> tag forwards requests to other Web components.

    • The <jsp:include ... /> tag dynamically includes pages from other Web components in the current page.

    • The <jsp:useBean ... /> tag is used to reference Java beans.

    • The <jsp:get/setProperty ... /> tag is used to access and change business data stored in Java beans.

In addition to the JSP directives there are a number of reserved JSP variables, most of which enable JSP developers to access the servlet environment and control classes. Some of these variables are listed in Table 5.1.

Table 5.1. Reserved JSP Variables

Variable

Servlet class

Description

request

HTTPRequest

Used to extract information to process requests from clients .

response

HTTPResponse

Used to send a response back to requesting clients.

out

ServletOutputBuffer

Used to write the response to the client.

session

HTTPSession

Used to maintain information across pages within a session.

application

ServletContext

Used to access the Web container.

config

ServletConfig

Used to retrieve servlet initialization parameters.

For technical information on developing JavaServer Pages in the context of the WebLogic Server 7.0, see "JavaServer Pages and Tag Libraries," p. 569 .




BEA WebLogic Platform 7
BEA WebLogic Platform 7
ISBN: 0789727129
EAN: 2147483647
Year: 2003
Pages: 360

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net