FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/JavaServer_Pages

Jakarta Server Pages

Contents
Overview
Syntax
    Directives, scriptlets, and expressions declaration
        Example
    Standard JSP Tags
        The useBean Tag
        Example
    Additional tags
        Jakarta Standard Tag Library
See also
    Servlet containers
    Java-based template alternatives
References
Further reading

JSP
Filename extension
.jsp, .jspx, .jspf
Internet media type

Overview

edit
The JSP Model 2 architecture

Architecturally, JSP may be viewed as a high-level abstraction of Jakarta Servlets. JSPs are translated into servlets at runtime, therefore JSP is a Servlet; each JSP servlet is cached and re-used until the original JSP is modified.

Jakarta Server Pages can be used independently or as the view component of a server-side model–view–controller design, normally with JavaBeans as the model and Java servlets (or a framework such as Apache Struts) as the controller. This is a type of Model 2 architecture.

JSP allows Java code and certain predefined actions to be interleaved with static web markup content, such as HTML. The resulting page is compiled and executed on the server to deliver a document. The compiled pages, as well as any dependent Java libraries, contain Java bytecode rather than machine code. Like any other .jar or Java program, code must be executed within a Java virtual machine (JVM) that interacts with the server's host operating system to provide an abstract, platform-neutral environment.

JSPs are usually used to deliver HTML and XML documents, but through the use of OutputStream, they can deliver other types of data as well.

The Web container creates JSP implicit objects like request, response, session, application, config, page, pageContext, out and exception. JSP Engine creates these objects during translation phase.

Syntax

edit

Directives, scriptlets, and expressions declaration

edit

JSPs use several delimiters for scripting functions. The most basic is <% ... %>, which encloses a JSP scriptlet. A scriptlet is a fragment of Java code that runs when the user requests the page.

Other common delimiters include <%= ... %> for expressions, where the scriptlet and delimiters are replaced with the result of evaluating the expression, and directives, denoted with <%@ ... %>.

Java code is not required to be complete or self-contained within a single scriptlet block. It can straddle markup content, provided that the page as a whole is syntactically correct. For example, any Java if/for/while blocks opened in one scriptlet must be correctly closed in a later scriptlet for the page to successfully compile. This allows code to be intermingled and can result in poor programming practices.

Content that falls inside a split block of Java code (spanning multiple scriptlets) is subject to that code. Content inside an if block will only appear in the output when the if condition evaluates to true. Likewise, content inside a loop construct may appear multiple times in the output, depending upon how many times the loop body runs.

Example

edit
\n<p>Counting to three:</p>\n<% for (int i = 1; i < 4; i++) { %>\n <p>This number is <%= i %>.</p>\n<% } %>\n<p>OK.</p>\n</syntaxhighlight>\n"},"5":{"wt":"<syntaxhighlight lang=\"html\">\n<p>Counting to three:</p>\n<p>This number is 1.</p>\n<p>This number is 2.</p>\n<p>This number is 3.</p>\n<p>OK.</p>\n</syntaxhighlight>\n"},"6":{"wt":"<p>Counting to three:</p>\n<p>This number is 1.</p>\n<p>This number is 2.</p>\n<p>This number is 3.</p>\n<p>OK.</p>\n"}},"i":0}}]}'>
The following would be a valid for loop in a JSP page:
JSP markupsHTMLRenders as
Counting to three:</p>\n<% for (int i = 1; i < 4; i++) { %>\n <p>This number is <%= i %>.</p>\n<% } %>\n<p>OK.</p>\n"}}'><p>Countingtothree:</p> <%for(inti=1;i<4;i++){%> <p>Thisnumberis<%=i%>.</p> <%}%> <p>OK.</p>
Counting to three:</p>\n<p>This number is 1.</p>\n<p>This number is 2.</p>\n<p>This number is 3.</p>\n<p>OK.</p>\n"}}'><p>Counting to three:</p> <p>This number is 1.</p> <p>This number is 2.</p> <p>This number is 3.</p> <p>OK.</p>

Counting to three:

This number is 1.

This number is 2.

This number is 3.

OK.

Standard JSP Tags

edit

The useBean Tag

edit

The JSP useBean tag enables the developer to access and create a Javabean. Although using the useBean tag looks similar to an HTML tag, all JSP tags for JavaBeans use XML syntax. Therefore the code containing the useBean tag is case-sensitive.

The useBean tag contains several attributes. The id attribute declares the name that is used for gaining access to the bean. The class attribute declares the package and class for the bean. The scope declares the object responsible for storing the bean. The value for the scope defines the duration for which the bean is available for the rest of the java application to use. The scope can be one of the following four values:

Further reading

edit