WEB开发网
开发学院网页设计JavaScript JSP 阅读

JSP

 2007-11-27 17:47:29 来源:WEB开发网   
核心提示:. Syntax Summaryjsp(SUN企业级应用的首选) Element Syntax Interpretation Notes jsp(SUN企业级应用的首选) Expression <%= expression %>Expression is evaluated and placed in ou
. Syntax Summaryjsp(SUN企业级应用的首选) Element Syntax Interpretation Notes jsp(SUN企业级应用的首选) Expression
<%= expression %>
Expression is evaluated and placed in output. xml(标准化越来越近了) equivalent is
<jsp(SUN企业级应用的首选):expression>
expression
</jsp(SUN企业级应用的首选):expression>
. Predefined variables are request, response, out, session, application, config, and pageContext (available in scriptlets also). jsp(SUN企业级应用的首选) Scriptlet
<% code %>
Code is inserted in service method. xml(标准化越来越近了) equivalent is
<jsp(SUN企业级应用的首选):scriptlet>
code
</jsp(SUN企业级应用的首选):scriptlet>
. jsp(SUN企业级应用的首选) Declaration
<%! code %>
Code is inserted in body of servlet class, outside of service method. xml(标准化越来越近了) equivalent is
<jsp(SUN企业级应用的首选):declaration>
code
</jsp(SUN企业级应用的首选):declaration>
. jsp(SUN企业级应用的首选) page Directive
<%@ page att=val %>
Directions to the servlet engine about general setup. xml(标准化越来越近了) equivalent is
<jsp(SUN企业级应用的首选):directive.page att=val\>. Legal attributes, with default values in bold, are:
  • import=package.class
  • contentType=MIME-Type
  • isThreadSafe=true|false
  • session=true|false
  • buffer=sizekb|none
  • autoflush=true|false
  • extends=package.class
  • info=message
  • errorPage=url
  • isErrorPage=true|false
  • language=java
jsp(SUN企业级应用的首选) include Directive
<%@ include file=url %>
A file on the local system to be included when the jsp(SUN企业级应用的首选) page is translated into a servlet. xml(标准化越来越近了) equivalent is
<jsp(SUN企业级应用的首选):directive.include
 file=url\>
.
The URL must be a relative one. Use the jsp(SUN企业级应用的首选):include action to include a file at request time instead of translation time. jsp(SUN企业级应用的首选) Comment
<%-- comment --%>
Comment; ignored when jsp(SUN企业级应用的首选) page is translated into servlet. If you want a comment in the resultant HTML, use regular HTML comment syntax of <-- comment -->. The jsp(SUN企业级应用的首选):include Action
<jsp(SUN企业级应用的首选):include    page=relative URL    flush=true/>
Includes a file at the time the page is requested. If you want to include the file at the time the page is translated, use the page directive with the include attribute instead. Warning: on some servers, the included file must be an HTML file or jsp(SUN企业级应用的首选) file, as determined by the server (usually based on the file extension). The jsp(SUN企业级应用的首选):useBean Action <jsp(SUN企业级应用的首选):useBean att=val*/> or
<jsp(SUN企业级应用的首选):useBean att=val*>
...
</jsp(SUN企业级应用的首选):useBean>
Find or build a Java Bean. Possible attributes are:
  • id=name
  • scope=page|request|session|application
  • class=package.class
  • type=package.class
  • beanName=package.class
The jsp(SUN企业级应用的首选):setProperty Action
<jsp(SUN企业级应用的首选):setProperty att=val*/>
Set bean properties, either explicitly or by designating that value comes from a request parameter. Legal attributes are
  • name=beanName
  • property=propertyName|*
  • param=parameterName
  • value=val
The jsp(SUN企业级应用的首选):getProperty Action
<jsp(SUN企业级应用的首选):getProperty    name=propertyName    value=val/>
Retrieve and output bean properties.   The jsp(SUN企业级应用的首选):forward Action
<jsp(SUN企业级应用的首选):forward    page=relative URL/>
Forwards request to another page.   The jsp(SUN企业级应用的首选):plugin Action
<jsp(SUN企业级应用的首选):plugin    attribute=value*>  ...</jsp(SUN企业级应用的首选):plugin>
Generates OBJECT or EMBED tags, as appropriate to the browser type, asking that an applet be run using the Java Plugin.  

3. Template Text: Static HTML

In many cases, a large percent of your jsp(SUN企业级应用的首选) page just consists of static HTML, known as template text. In all respects except one, this HTML looks just like normal HTML, follows all the same syntax rules, and is simply passed through to the client by the servlet created to handle the page. Not only does the HTML look normal, it can be created by whatever tools you already are using for building Web pages. For example, I used Allaires HomeSite for most of the jsp(SUN企业级应用的首选) pages in this tutorial.

The one minor exception to the template text is passed straight through rule is that, if you want to have <% in the output, you need to put <\% in the template text.

4. jsp(SUN企业级应用的首选) Scripting Elements

jsp(SUN企业级应用的首选) scripting elements let you insert Java code into the servlet that will be generated from the current jsp(SUN企业级应用的首选) page. There are three forms:
  1. Expressions of the form <%= expression %> that are evaluated and inserted into the output,
  2. Scriptlets of the form <% code %> that are inserted into the servlets service method, and
  3. Declarations of the form <%! code %> that are inserted into the body of the servlet class, outside of any existing methods.
Each of these is described in more detail below.

4.1 jsp(SUN企业级应用的首选) Expressions

A jsp(SUN企业级应用的首选) expression is used to insert Java values directly into the output. It has the following form:
<%= Java Expression %>
The Java expression is evaluated, converted to a string, and inserted in the page. This evaluation is performed at run-time (when the page is requested), and thus has full access to information about the request. For example, the following shows the date/time that the page was requested:
Current time: <%= new java.util.Date() %>
To simplify these expressions, there are a number of predefined variables that you can use. These implicit objects are discussed in more detail later, but for the purpose of expressions, the most important ones are:
  • request, the HttpServletRequest;
  • response, the HttpServletResponse;
  • session, the HttpSession associated with the request (if any); and
  • out, the PrintWriter (a buffered version of type jsp(SUN企业级应用的首选)Writer) used to send output to the client.
Heres an example:
Your hostname: <%= request.getRemoteHost() %>
Finally, note that xml(标准化越来越近了) authors can use an alternative syntax for jsp(SUN企业级应用的首选) expressions:
<jsp(SUN企业级应用的首选):expression>Java Expression</jsp(SUN企业级应用的首选):expression>
Remember that xml(标准化越来越近了) elements, unlike HTML ones, are case sensitive. So be sure to use lowercase.

4.2 jsp(SUN企业级应用的首选) Scriptlets

If you want to do something more complex than insert a simple expression, jsp(SUN企业级应用的首选) scriptlets let you insert arbitrary code into the servlet method that will be built to generate the page. Scriptlets have the following form:
<% Java Code %>
Scriptlets have access to the same automatically defined variables as expressions. So, for example, if you want output to appear in the resultant page, you would use the out variable.
<% String queryData = request.getQueryString();out.println(Attached GET data:  + queryData); %>
Note that code inside a scriptlet gets inserted exactly as written, and any static HTML (template text) before or after a scriptlet gets converted to print statements. This means that scriptlets need not contain complete Java statements, and blocks left open can affect the static HTML outside of the scriptlets. For example, the following jsp(SUN企业级应用的首选) fragment, containing mixed template text and scriptlets
<% if (Math.random() < 0.5) { %>Have a <B>nice</B> day!<% } else { %>Have a <B>lousy</B> day!<% } %>
will get converted to something like:
if (Math.random() < 0.5) {   out.println(Have a <B>nice</B> day!);} else {   out.println(Have a <B>lousy</B> day!);}
If you want to use the characters %> inside a scriptlet, enter %\> instead. Finally, note that the xml(标准化越来越近了) equivalent of <% Code %> is
<jsp(SUN企业级应用的首选):scriptlet>Code</jsp(SUN企业级应用的首选):scriptlet>

4.3 jsp(SUN企业级应用的首选) Declarations

A jsp(SUN企业级应用的首选) declaration lets you define methods or fields that get inserted into the main body of the servlet class (outside of the service method processing the request). It has the following form:
<%! Java Code %>
Since declarations do not generate any output, they are normally used in conjunction with jsp(SUN企业级应用的首选) expressions or scriptlets. For example, here is a jsp(SUN企业级应用的首选) fragment that prints out the number of times the current page has been requested since the server booted (or the servlet class was changed and reloaded):
<%! private int accessCount = 0; %>Accesses to page since server reboot: <%= ++accessCount %>
As with scriptlets, if you want to use the characters %>, enter %\> instead. Finally, note that the xml(标准化越来越近了) equivalent of <%! Code %> is
<jsp(SUN企业级应用的首选):declaration>Code</jsp(SUN企业级应用的首选):declaration>

5. jsp(SUN企业级应用的首选) Directives

A jsp(SUN企业级应用的首选) directive affects the overall structure of the servlet class. It usually has the following form:
<%@ directive attribute=value %>
However, you can also combine multiple attribute settings for a single directive, as follows:
<%@ directive attribute1=value1               attribute2=value2              ...              attributeN=valueN %>
There are two main types of directive: page, which lets you do things like import classes, customize the servlet superclass, and the like; and include, which lets you insert a file into the servlet class at the time the jsp(SUN企业级应用的首选) file is translated into a servlet. The specification also mentions the taglib directive, which is not supported in jsp(SUN企业级应用的首选) version 1.0, but is intended to let jsp(SUN企业级应用的首选) authors define their own tags. It is expected that this will be the main new contribution of jsp(SUN企业级应用的首选) 1.1.

5.1 The jsp(SUN企业级应用的首选) page Directive

The page directive lets you define one or more of the following case-sensitive attributes:
  • import=package.class or import=package.class1,...,package.classN. This lets you specify what packages should be imported. For example:
    <%@ page import=java.util.* %>
    The import attribute is the only one that is allowed to appear multiple times.
  • contentType=MIME-Type or
    contentType=MIME-Type; charset=Character-Set
    This specifies the MIME type of the output. The default is text/html. For example, the directive
    <%@ page contentType=text/plain %>
    has the same effect as the scriptlet
    <% response.setContentType(text/plain); %>
  • isThreadSafe=true|false. A value of true (the default) indicates normal servlet processing, where multiple requests can be processed simultaneously with a single servlet instance, under the assumption that the author synchronized access to instance variables. A value of false indicates that the servlet should implement SingleThreadModel, with requests either delivered serially or with simultaneous requests being given separate servlet instances.
  • session=true|false. A value of true (the default) indicates that the predefined variable session (of type HttpSession) should be bound to the existing session if one exists, otherwise a new session should be created and bound to it. A value of false indicates that no sessions will be used, and attempts to access the variable session will result in errors at the time the jsp(SUN企业级应用的首选) page is translated into a servlet.
  • buffer=sizekb|none. This specifies the buffer size for the jsp(SUN企业级应用的首选)Writer out. The default is server-specific, but must be at least 8kb.
  • autoflush=true|false. A value of true, the default, indicates that the buffer should be flushed when it is full. A value of false, rarely used, indicates that an exception should be thrown when the buffer overflows. A value of false is illegal when also using buffer=none.
  • extends=package.class. This indicates the superclass of servlet that will be generated. Use this with extreme caution, since the server may be using a custom superclass already.
  • info=message. This defines a string that can be retrieved via the getServletInfo m

Tags:JSP

编辑录入:coldstar [复制链接] [打 印]
赞助商链接