Java Web Learning (7): JSP basic syntax

Source: Internet
Author: User
Tags html comment

a JSP page can be divided into several parts:

1) static data, such as HTML;

2) JSP directives, such as include directives;

3) JSP script elements and variables;

4) JSP action;

5) User-defined label;

A static data

The content and output of the static data in the input file is exactly the same as the content of the HTTP response. At this point, the JSP input file will be a non-inline

HTML page for Java or action. Also, the client receives the same response for each request.

The essence of this static data is the HTML document.

two JSP directives

The JSP directive controls how the JSP compiler generates the servlet, and the following are the available directives:

(1) Page directive

The page directive is usually at the top of the JSP page, and the same page can have multiple page directives. It is important to note that in the same JSP file only

There are "import" Import page directives that can be used multiple times.

The page directive syntax is formatted as

<%@ Page Property 1 = "Property Value" Property 2 = "Property value 1, property value 2" ... Property N= "Property value N"%>

The page directive has several options:


The page directive uses the sample:

<%@ page import= "java.util.*"%>//example Import sample

<%@ page contenttype= "text/html"%>//example contentType pages Type sample

<%@ page Iserrorpage=false%>//example for non error pages sample without errors

<%@ page Isthreadsafe=true%>//example for a threadsafe JSP thread path JSP example

Let's look at the example: output the current date in a JSP page

date.jsp page Source:

<% @page import= "Java.text.SimpleDateFormat"%><% @page import= "java.util.Date"%><%@ page language= " Java "contenttype=" text/html; Charset=utf-8 "    pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Operation Result:


In the above code, we use the page directive to import Java.text.SimpleDateFormat and java.util.Date, and also use the page directive to specify

The character encoding format of the current page and the default scripting language Java used.


For more page instructions, we learn more about JSP, will use to get, the gradual learning process will use a lot of page instructions.

(2) include directive

The include directive refers to embedding an external file in the current JSP file and parsing the JSP statements in the page. The include directive, which is used to

The current page inserts the contents of a static file. This file can be a JSP, HTML, text, or Java program.

The include directive tells the JSP compiler to include the other file completely in the current file. The effect is as if the contents of the included file are straight

is pasted into the current file. This feature is similar to what the C-language preprocessor provides. The extension of the included file is generally "JSPF" (that is,

Jspfragment, jsp fragment):

The include directive syntax format is:

<%@ include file= "somefile.jsp"%>

Let's look at an example:

We use the page date.jsp that shows the current date as the embedded J file.

Code to test the include.jsp page of the include directive:


Test results:


After delving into the JSP, we compare the difference between the include directive and the include action.

(3) taglib directive

The taglib directive refers to using a tag library to define a new custom label, enabling custom behavior in a JSP page. The taglib directive is a tag library directive that describes

The JSP tag library to use. The directive needs to specify a prefix prefix (similar to the C + + namespace) and a description URI for the tag library. Declaring users to use

Customize the label to import the tag library descriptor file to the JSP page.

The syntax format for the TAGLIB directive is:

<%@ taglib prefix= "Myprefix" uri= "Taglib/mytag.tld"%>

URI attribute: Locates the location of the tag Library descriptor. A tag library descriptor that uniquely identifies and prefixes, and can use absolute or relative URLs.

Tagdir property: Indicates that the prefix will be used to identify the label file in the Wev-inf/tags directory.

Prefix property: The prefix of the label, which distinguishes multiple custom labels. You cannot use a reserved prefix and an unprecedented prefix to follow the naming conventions for XML namespaces.

This I have not yet used, but can draw on other people's article: JSP use Taglib.

three JSP annotations

There are three types of annotations on the JSP page:

1) HTML Comment:

<!--HTML comments-//client visible by viewing source code

2) JSP Comments:

<%--jsp Comment--%>//client not visible, server-side comment

JSP script Comments:

Single-line Comment

/**/Multi-line Comment//client not visible in Java statement: server-side comment client does not see

We will also demonstrate on the JSP page:


Results:


four JSP script

A JSP script is a Java code that executes on a JSP page.

(1) Standard script variables

The following are the always available script variables:

Out–jspwriter data used to write to the response stream

Page–servlet itself

pagecontext– a PageContext instance includes data associated with the entire page, a given HTML page can be between multiple JSPs

Passed.

Request–httprequest Object

Response–httpresponse Object

session– objects used to keep client-to-server connections

The above-mentioned object variables are often used in the future. The following will be explained in detail.

(2) script elements

There are three basic scripting elements that enable Java code to plug into a servlet directly.

1) JSP Declaration

Define variables or methods in the JSP page. The declaration label puts the definition of a variable in the class body of the Java servlet. A static data member can also

So defined.

the JSP declaration syntax format is: <%! Java Code%>

Example:<%! int serverinstancevariable = 1;%>

Instance:


2) JSP script

A JSP script is a Java code that executes on a JSP page. in the _jspservice () method of the Java Servlet class, put the contained  statement.

The JSP script syntax format is: <% java code%>

Example: <% int localstackbasedvariable = 1; Out.println (localstackbasedvariable); %>

Instance:


3) JSP expression

A JSP expression is an expression that executes in a JSP page. in the Java servlet class, put the expression to be assigned, the expression note You cannot use a semicolon to knot

Tail.
JSP expression Syntax format: <%= expression%>//Note: The expression does not end with a semicolon

Example: <%= "expanded inline data" + 1%>

Instance:


The last browser of the above example views the results:


Five JSP actions

A JSP action is a series of XML tags that can invoke functionality built into a network server. The JSP provides the following actions:


For the JSP action, we will explain in detail later.

Six The life cycle of a JSP page

JSP page Flow:


JSP life cycle

The Jspservice () method is called to process the client's request. For each request, the JSP engine creates a new thread to process the request. If there is

When multiple clients request the JSP file at the same time, the JSP engine creates multiple threads, and each client requests a thread. Multi-threaded execution can

To greatly reduce the resource requests to the system, increase the concurrency and response time of the system. However, it is also important to note that multi-threading programming brings the problem of synchronization due to the

The servlet is always resident in memory, so the response is very fast.

Instance:


Results:


Explain:

The Jspinit () method is executed at the same time when a bytecode file is generated when the JSP engine resolves the first request:

1) The JSP engine first converts the JSP file into a Java source file (Servlet), and if it finds any syntax errors in the JSP file at the time of conversion, the conversion

Process will be interrupted and output error messages to the server and client.

2) If the conversion succeeds, the JSP engine compiles the Java source file into the corresponding class file with Javac.

3) Create an instance of the servlet (the conversion result of the JSP page), the servlet's Jspinit () method is executed, and the Jspinit () method in the servlet's life

The cycle is executed only once.

4) The Jspservice () method is called to process the client's request. For each request, the JSP engine creates a new thread to process the request. If

When multiple clients request the JSP file at the same time, the JSP engine creates multiple threads. Each client request corresponds to a thread. Execute in multithreaded fashion

Can greatly reduce the resource requirements of the system, improve the system's concurrency and response time. However, you should also pay attention to the multi-threading programming limitations, because the

The servlet is always resident in memory, so the response is very fast.




Java Web Learning (7): JSP basic syntax

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.