JSP tutorial (1)

Source: Internet
Author: User

JSP Overview

JSP (iava Server Pages) is a dynamic web page creation technology developed by Sun in the Java language. It allows you to separate Dynamic and Static HTML in Web pages. You can use common handy tools to write HTML statements as usual. Then, embed the dynamic part with special tags, which often start with "<%" and end with "%>. For example, there is a JSP page:

<HTML>

<Head> <title> JSP tutorial </title>

<Body>

<I> <% Out. println ("Hello World"); %> </I>

</Body>

It will output "Hello World ".

Generally, you need to take the file ". jsp" as the extension and place it under any path where you can place a normal web page. Although the JSP file looks more like an HTML file than a Servlet File, in fact, it will be converted to a Servlet File, where the static html is only used to output the information returned by the servlet service method. If the JSP pages has been converted to Servlet and the servlet is compiled and loaded (at the first request), when you request this JSP page again, you will not be able to detect a transient delay. Pay attention to this phenomenon. Some Web servers allow you to define aliases for them, as if a URL points to an HTML, but in fact it points to a servlet or JSP pages.

Construct a JSP page. In addition to embedded rule HTML, there are three main JSP elements: scripting elements, directives, and actions. with scripting elements, you can define the part that is finally converted to servlet. Directives allows you to control the overall structure of this servlet, while actions allows you to specify reusable existing components. In addition, you can also control the running of the JSP Engine. To simplify scripting elements, you can take advantage of some predefined variables, such as request.

This tutorial is based on the latest version 1.1 of JSP. Its syntax is summarized in the following table. Its detailed usage will be explained in detail in subsequent courses.

JSP Elements
Syntax
Explanation

JSP expression
<% = Expression %>
Expression is used for calculation and output.
<JSP: expression> Expression </jsp: expression>. The predefined variables that can be used include request, response, out, session, application, config, and pagecontext (also available in sriptlets ).

JSP scriptlet
<%Code%>
Insert the code for the service.
<JSP: scriptlet> Code </jsp: scriptlet>

JSP Declaration
<! % Code %>
The servlet code is not a service method.
<JSP: Declaration> Code </jsp: Declaration>

JSP page Directive
<% @ Page ATT = "Val" %>
The path to the servlet engine.
<JSP: Directive. Page ATT = "Val" \>. The following are valid attributes (the default value is bold ):

L import = "package. Class"

L contenttype = "mime-type"

L isthreadsafe = "True | false"

L session = "True | false"

L buffer = "sizekb | none"

L autoflush = "True | false"

L extends = "package. Class"

L info = "message"

L errorpage = "url"

L iserrorpage = "True | false"

L language = "Java"

JSP include Directive
<% @ Include file = "url" %>
When the JSP page is translated into a servlet, it will be included in the files on the local system.
<JSP: Directive. Include File = "url" \> This URL must be relative. When the page is requested, call "JSP: include action.

JSP comments
<% -- Comment -- %>
When JSP is converted to servlet, it is ignored.
<-- Comment -->

The JSP: include action
<JSP: Include page = "relative URL" Flush = "true"/>
Transfer the file when the page is requested (requested.
If you want to include the file when the page is converted (translated), use the aforementioned direve ve instead. Warning on some servers, the contained file can only be HTML or JSP, which is generally determined by the file extension name.

The JSP: usebean action
<JSP: usebean ATT = Val */> or <JSP: usebean ATT = Val *>
.....
</Jsp: usebean>
Find or generate a Java Bean.
Possible attributes are:

L id = "name"

L scope = "Page | request | session | application"

L class = "package. Class"

L type = "package. Class"

L beanname = "package. Class"

The JSP: setproperty action
<JSP: setproperty ATT = Val */>
Set bean properties by explicitly specifying or using the request parameters.
Valid attributes:

L name = "beanname"

L property = "propertyname | *"

L Param = "parametername"

L value = "Val"

The JSP: getproperty action
<JSP: getproperty name = "propertyname" value = "Val"/>
Retrieves and outputs bean attributes.

The JSP: forward action
<JSP: Forward page = "relative URL"/>
Request another page.

The JSP: plugin action
<JSP: plugin attribute = "value" *>
</Jsp: plugin>
Generate the object or embed tag of a specific browser to specify the Java Plug-in (plugin) used to run the applet ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.