"JSP" JSP Basic Learning Record (i)

Source: Internet
Author: User
Tags html comment

 Order:

From implementation to now, it has been mainly in. NET, but occasionally in some other language projects. Recently, a Java Web project needs to be developed two times, has not studied JSP so bought a few books self-study to try. The reference is "lightweight Java EE Enterprise Application Practice (4th Edition)".

 Development environment:

System version: Win7 x64

JDK Version: 1.8

IDE version: Eclipse Java EE IDE for WEB Developers (version:mars.1 release (4.5.1))

Tomcat version: 8.0

1.1, the basic principle of JSP:

The nature of the JSP is the servlet, which, when the user sends a request to the specified servlet, uses the output stream to dynamically generate the HTML page, including the HTML tag for each static page and all the content that appears in the HTML page.

The development efficiency of servlets is extremely low due to the large number of HTML tags, a large number of static text passes, etc. All the performance logic, including layout, color and image, must be coupled in Java code, which is really patience. JSP appears to compensate for this deficiency, JSP by embedding Java code in the standard HTML page, its static part without Java program control, only those who need to read from the database or need to dynamically generate the content of the page, the use of Java script controls.

As can be seen from the above introduction, the content of the JSP page consists of the following two parts.

(1) Static parts: standard HTML tags, static page content, and the same content as static HTML pages.

(2) Dynamic section: Content controlled by Java, which is dynamically generated by Java scripts.

Note: You are not qualified for beginners to summarize these principles, so the above is introduced as reference books

1.1.1, the classic entry code Hello World:

  

1 <%@ Page Language="Java"ContentType="text/html; charset=iso-8859-1"2 pageencoding="iso-8859-1"%>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">4 <HTML>5 <Head>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1">7 <title>Hello World</title>8 </Head>9 <Body>Ten     <% One Out.print ("Hello world!"); A     %> - </Body> - </HTML>

<% and%> are wrapped in Java code, out of the JSP's built-in object. Out.print to output pinned content to HTML, the audit element can send the following content:

  

The nature of a JSP page is a servlet (a special Java class), so each page is a servlet instance, and the JSP page is compiled by the system into Servlet,servlet, which is then responsible for responding to user requests.

  Summarize:

(1) The JSP file must be run within the JSP server.

(2) The JSP file must be generated by a servlet to execute.

(3) The first visitor for each JSP page is slow because it must wait for the JSP to compile into a servlet.

(4) The JSP page visitors do not need to install any client, or even run the Java environment, because the JSP page output to the client is a standard HTML page.

 1.2, 4 basic syntax of JSP:

 

1.2.1, JSP comments:

The comments of the JSP are not output to the client through HTML, which means that the client service views the JSP annotations through the audit element. The JSP annotation format is <%--JSP comment content--%>

The corresponding HTML comment, compared to the HTML comment client, can view the comment content through the audit element. HTML comment format is <!--HTML comment content--! >

 

 1.2.2, output JSP expression:

  

1 <%@ Page Language="Java"ContentType="text/html; Charset=utf-8"2 pageencoding="UTF-8"%>3 <!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd ">4 <HTML>5 <Head>6 <Metahttp-equiv= "Content-type"content= "text/html; charset=iso-8859-1">7 <title>Output JSP expression</title>8 </Head>9 <Body>Ten     <%! One         Private StringSayHello () { A return"Hello world!"; -         } -     %> the     <P>SayHello:<%=SayHello ()%></P> - </Body> - </HTML>

Output the value of the Java expression through <%= and%>. It is important to note that the SayHello (), defined above, is wrapped by <%! and%>. And the output expression cannot have a semicolon.

  <%! declaration statement%>: General declaration of global variables, constants, methods, classes

<% Java code%>: which can contain local variables, Java statements

<%=java code%>: Output Java expression

1.3, the JSP 3 compile instructions: 

Page: the directive is for the current page

Include: to include another page on top

Taglib: Used to define and access custom labels

Syntax format for using compile Directives: <%@ compiler directive Name Property name = "Property value" ...%>

1.3.1, page directive:

The page directive is usually at the top of the JSP page, and a JSP page can use multiple page directives. The properties of the page directive are as follows:

Language: Declares the kind of scripting language used by the current JSP page, because the page is a JSP page, the value of which is usually Java, and the default value of this property is Java, so it is usually not necessary to set.

Extends: Specifies the parent class inherited by the Java class that the JSP page compiles from, or the interface that is implemented.

Import: for importing packages. The following packages are automatically imported by default and do not need to display imports. The default imported packages are: java.lang.*, javax.servlet.*, javax.serlet.jsp.*, javax.servlet.http.*.

Session: Set whether the JSP page requires an HTTP Session.

Buffer: The size of the pinned output buffer. JSP built-in object for output buffers: Out user caches the output of the JSP page to the client's browser, the default value is 8KB, can be set to none, or it can be set to a different value, in kilobytes.

AutoFlush: The content of the output buffer needs to be forced when the output buffer is about to overflow. is normal when set to true, and if set to False, an exception is generated when buffer overflows.

Info: Set the information for the JSP program, as well as its description, which can be obtained through the Servlet.getservletinfo () method. If you are in a JSP page, you can call the Getservletinfo () method directly to get the value. Because the nature of the JSP page is the servlet.

ErrorPage: pinned error Handling page. If this page generates an exception or an error, and the JSP page does not have a corresponding processing code, the JSP page created by the property is automatically invoked.

Iserrorpage: Sets whether this JSP page is an error handler. If the page itself is already an error-handling page, it is usually not necessary to pin the ErrorPage property.

ContentType: Used to set the file format and encoding character set of the generated Web page, which is the MIME type and page character set type, the default MIME type is text/html; The default character set type is iso-8859-1.

Pageencoding: Specifies the encoding character set for the generated Web page.

1.3.2, include directives:

  With the include directive, you can sneak an external file into the current JSP file and parse the JSP statements in the page. This is a static include statement that will include other instructions from the target page, but the dynamic include will not. Include can contain both static text and dynamic JSP pages. The static include compilation directive will include the included page into this page, fused into a page, so the inclusion of the page does not even need to be a complete page. include compiler directive syntax: <% @include file= "Relativeurlspec"%>.

If the file being infiltrated often needs to be changed, it is recommended to use the <jsp:include> operation instruction as it is a dynamic include statement.

<%include file= "a.jsp"%>: is added at compile time, so-called Static, is at the time of compiling a.jsp code to join in the compilation, and then run.
<jsp:include page= "a.jsp"/>: is added at runtime, the so-called dynamic, is compiled separately, in the runtime to join in, after the display, this include, can pass parameters.

Note: Static inclusions will also include the compilation instructions for the included pages, which will be an error if the compilation instructions for the next year's brother page conflict.

 Tail:

The above narrative is more, most of the reference books cited in the preface. There are also parts that are obtained through search engine queries.

"JSP" JSP Basic Learning Record (i)

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.