Java Web Foundation Summary VIII--jsp Foundation

Source: Internet
Author: User

Java Web Foundation Summary VIII--jsp FoundationA. What is a JSP?

JSP is the abbreviation for Java serverpages, which, like the servlet, is the technology used to develop dynamic Web resources. It is cumbersome to cobble together the output HTML code in a servlet. The biggest feature of JSP is that writing JSP is like writing HTML, but HTML can only provide static data for the user, and JSP technology allows to nest Java code in the page and develop dynamic resources.

Now with the front-end more and more important, many companies will take a front-end separation of development patterns. That is, the backend only provides interfaces that return formats such as JSON, while front-end developers work with JSON data through requests for back-end interfaces to display them accordingly. JSP is seldom used in this way. But learning the Java Web, you cannot learn JSP.

Both JSP and servlet can be used to develop dynamic Web resources. But in the actual development, the servlet is usually used as the controller component in the Web application, and the JSP technology is used as the data display template. That is, the servlet is only responsible for responding to requests to generate data, and the data through the forwarding technology to JSP, the data display JSP to do.

two. The principle of JSP

The first thing to be clear is that the JSP is also a servlet, so it works like a servlet. When each JSP page is accessed for the first time, the Web container will give the request to the JSP engine (a Java program) to process. The JSP engine first translates the JSP into a _jspservlet (essentially a servlet) and then invokes it in the way the servlet calls.

Because the JSP is translated into a servlet on the first visit, the first access is usually slower, but the second access, if the JSP engine finds that the JSP has not changed, is no longer translated, but is called directly, so the execution efficiency of the program will not be affected.

Of course, JSPs can also be configured to launch loads for Web apps. JSP can also configure the mapping path in Web. Xml like a servlet, but it usually accesses the appropriate JSP through a servlet.

For example:







<servlet-name> helloworldjspservlet</servlet-name>



three. Introduction to the syntax of JSP1. JSP elements

The JSP element is the HTML content in the JSP page. You can define HTML tags in a JSP page. So it defines the basic skeleton of the Web page, defines the structure and appearance of the page. such as head, body.

2. JSP Script expression

JSP script expressions (expression) are used to output program data to the client, which is simply the value of the Java variable nested in the output expression. The syntax is: <%= variable or expression%>. For example, the current time: <%= new Java.util.Date ()%>. When the JSP engine translates the script expression, it turns the program data into a string and then uses the Out.print (...) in the appropriate location. method to lose data to the client. It is important to note that a variable or expression in a JSP script expression cannot have a semicolon after it.

3. JSP Script fragment

The JSP script fragment (scriptlet) is to write multiple lines of Java code in a JSP page, although usually we do not nest too much Java code in a JSP file. The syntax is: <% java code%>

It is important to note that only Java code can appear in the JSP script fragment and no other template elements can appear because the JSP engine translates the Java code in the JSP script fragment into the servlet's _jspservice method in the translation JSP page. So the Java code in the JSP script fragment must strictly follow the Java syntax, and unlike JSP script expressions, each execution statement must end with a semicolon.

You can have multiple script fragments in a JSP page that can embed text, HTML tags, and other JSP elements between two or more script fragments.

For example:


String S1 = "Hello";

Out.println (S1);


<p>hello world!</p>


String s2 = "world!";

Out.println (S1+S2);


The code in multiple script fragments can be accessed from one another as if all the code were placed in a pair of <%%>. such as: Out.println (S1+S2); So the Java statements in a single script fragment can be incomplete, but the result of a combination of multiple script fragments must be a complete Java statement.

4. JSP Declaration

Unlike JSP declarations and JSP script fragments, the Java code in the JSP script fragment is translated into the servlet's service method by default, and the Java code in the JSP declaration is translated to the outside of the _jspservice method. Grammar:

<%! Java Code%>

Therefore, JSP declarations can be used to define the static code blocks, member variables, and methods of the servlet program that the JSP page transforms into. Multiple static code blocks, variables, and functions can be defined in a JSP declaration or individually in multiple JSP declarations. JSP implicit objects are scoped only to the _jspservice method of the servlet, so these implicit objects cannot be used in JSP declarations. For example:




System.out.println ("helloworld!");



5.JSP Comments

The syntax for JSP annotations is: <%--annotation information--%>. When the JSP engine translates a JSP page into a servlet program, the contents of the JSP page are commented on.

6. Introduction to JSP directives

Basic syntax format for JSP directives: <%@ directive Property name = "Value"%>

Example: <% @page contenttype= "Text/html;charset=utf-8" language= "java"%>

Here are a few of the more commonly used JSP directives.

(1). Page

The role of the page directive is to define the various properties of JSP pages, it is the entire JSP page, so in order to maintain the readability of the program and follow good programming habits, the page instruction is generally placed in the entire JSP page start position.

The full syntax of the page directive defined in the JSP 2.0 specification:

<%@ page

[language= "Java"]

[extends= "Package.class"]

[import= "{package.class | package.*}, ..."]

[session= "True |false"]

[buffer= "none | 8kb |sizekb"]

[autoflush= "True |false"]

[isthreadsafe= "True |false"]

[info= "Text"]

[errorpage= "Relative_url"]

[iserrorpage= "True |false"]

[contenttype= "MimeType [; Charset=characterset]" | "text/html; Charset=iso-8859-1"]

[Pageencoding= "CharacterSet | Iso-8859-1 "]

[iselignored= "True |false"]


(2). Include

The include directive is the introduction of other JSP pages, and if you use the include directive to introduce other JSP pages, the JSP engine translates the two jsps into a servlet. So the introduction of include directives is often referred to as static ingestion.

Syntax: <% @include file= "XX.JSPF"%>

The file property is used to specify the relative path of the introduced files. It is important to note that the value of the file property must use a relative path, which, if preceded by "/", is relative to the current Web application's root directory (note that it is not the site root), otherwise it represents relative to the active file. The file being introduced can use any extension, even if its extension is the html,jsp engine that handles the contents of the JSP page as it is handled, the JSP specification suggests using. JSPF (JSP fragments) as the extension of the static ingest file. Since the use of the include directive will involve 2 JSP pages and will translate 2 jsps into a servlet, the instructions for these 2 JSP pages do not conflict with each other.

(3). Taglib

The taglib directive is used to import tag libraries in JSP pages. For example:

<% @taglib uri= "Http://" prefix= "C"%>

Four. The 9 built-in objects of the JSP and 4 of these domain objects.

When the JSP engine invokes the _jspservlet of a JSP, it passes or creates 9 web-development-related objects for _jspservlet to use. JSP technology designers for developers to write JSP pages when they get references to these Web objects, deliberately defined 9 corresponding variables, developers in the JSP page through these variables can quickly get these 9 large object references. It is important to note that the scope of the JSP implicit object is limited to the _jspservice method of the servlet, so these implicit objects cannot be used in JSP declarations.

1.9 Built-in objects for JSP

Request: This object is HttpServletRequest, not much narration

Response: This object is HttpServletResponse.

Config: This is servletconfig, which has been introduced in the servlet to configure some initialization parameters for the servlet, scoped to a servlet.

Application: This is ServletContext, as mentioned earlier, the scope is the entire Web application.

Exception: Exception-related, used to handle exceptions.

Session: It is httpsession, not much to say.

Page: This is the This object, which represents the current page.

Out:out an implicit object is used to send text data to the client. The Out object is returned by calling the Getout method of the PageContext object, and its role and usage is very similar to the PrintWriter object returned by the Servletresponse.getwriter method. The type of an out implicit object in a JSP page is jspwriter,jspwriter equivalent to a printwriter with a cache function, and the buffer property of the page directive that sets the JSP page can resize its cache or even turn it off.

Pagecontext:pagecontext object is a very important object of JSP, it represents the environment of JSP page, this object not only encapsulates the reference to other 8 big implicit object, it is also a domain object itself, it can be used to save the data.     Also, this object encapsulates some common operations that are often involved in web development. It has a very important method: The Findattribute method, which is to find the attributes in each domain. The EL expression is implemented through it. The EL expression statement, when executed, invokes the Pagecontext.findattribute method, using the identifier as the keyword, to find the corresponding object from page, request, session, and application four fields, and then returns the corresponding object, looking for You can avoid exceptions by returning an empty string instead of NULL.

2.4 domain objects for the Java Web:

Some properties can be saved in these four fields, and the value of the property can be taken out by name.

PageContext (called page field): The scope of the action is the smallest, is the current JSP page

Request (called the request domain): The scope of the action is one request.

Session (called Session field): Is the session, the scope is a client browser.

ServletContext (called application domain): The largest scope for the entire Web application.

Java Web Foundation Summary VIII--jsp Foundation

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: 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.