J2EE--JSP's study

Source: Internet
Author: User
Tags instance method

"Click to view the original"

JSP (Java Server page) Java service-side web page, is a Dynamic Web page technology, which and. NET, the ASP functions are the same. JSP is a set of standards set by sun, and is one of the 13 specifications of Java EE. Say something not quite right "JSP is an upgrade to HTML, an extension, that is, static--> dynamic."

First, good, simple to understand the JSP concept, we look at the principle and nature of JSP:


1, first look at an example, we visit the home page This index.jsp, the program execution order:



2, the principle and nature of JSP derived from the above example:

The Index_jsp.class class in the previous example inherits Httpjspbase, and Httpjspbase inherits HttpServlet, so the JSP essence is a servlet, exactly the same as the servlet. JSP has three stages: translate (once), compile (once), run (multiple times). This explains why the first time we visit the JSP page is slow to run, and later access is very fast reason. But the two are very different: JSP extraction data to do the page display, that is, the so-called view (display layer), and the servlet mainly completes the business logic processing and resource jump, which is called Controller (control layer).


Second, since it is one of the 13 specifications of Java EE, then what is the basic syntax of JSP?



These are the specifications that need to be followed, and nine of them are particularly important, and here's a brief introduction to these pieces of knowledge:


1,scriptlet (Little Foot): is the use of <%%> in the JSP file, the Java program that appears in the symbol is placed in the Servlet's service method after translation. In other words, we can put Java programs where we want knowledge.


2,declaration (Declaration): Is the use of <% in JSP files. %> the Java program that appears within the symbol will be stored in a position tied to the Servlet's service method. So you can declare a static variable, static method, static code block, instance code block, instance variable, instance method within the symbol.


3,expression (expression): is the use of <%=%> in the JSP file, the equivalent of Out.print (), the general need for us to print the variable is needed here, because all the JSP inside is a string.


4, Directives (Directive): Designed for the JSP engine, they do not directly produce any visible output, but only tell the engine how to handle the rest of the JSP page, such as some encoding, response, and so on.

A, the most common page instruction <% @page%>: Import property (translate generated import statements in the Java language) <% @page import= "java.util.date,java.util.arraylist The%> ContentType Property (Specifies the JSP page response content Type) <% @page contenttype= "text/html"%> pageencoding attribute (specifies the page character encoding of the JSP page response) <%@ Page pageencoding= "GB18030"%> Session property (Specifies whether the current JSP page can use the session's built-in object) <% @page session= "false"%> Session built-in objects are not available <% @page session= "true"%> session built-in objects available (default) ErrorPage properties (Specify the resource path to jump after the current JSP page has been incorrectly) Iserrorpage Property (specifies that the current JSP page is an error page to use the built-in object exception) Iselignored property to specify whether an El expression is ignored in the current page, true to ignore, or false to ignore

B,include instruction <% @include%>:

Include directives represent static inclusions, such as the <% @include file= "b.jsp"%> in a.jsp; This means merging the Java source program after the a.jsp translation with the Java source of the b.jsp translation, and then compiling it to generate a. class file. We usually use this approach to reuse the page. A bit similar. NET in the master page.

C,taglib instruction <% @taglib%>:

In order to make JSP look more professional (only to do the page display), reduce the number of Java code in the JSP page, we introduced a tag library. The tag library here can be the JSTL function library, or we can write it ourselves. About JSTL reference jstl Baidu Branch Hundred.

5, Action (action)

A,forward Action:

<jsp:forward page= "/2.jsp"/>

Forwarding: equivalent in servlet

Request.getrequestdispatcher ("/2.jsp"). Forward (Req,resp) method.


B,include Action (note the contrast with include directives, one is static and one is dynamic)

<jsp:includepage= "/b.jsp" ></jsp:include>

Include action represents dynamic inclusion, if the above code is written in the a.jsp page, the operation principle is to a.jsp file Translate into Java source program and compile it to generate A.class file, b.jsp file translation to generate Java source program, compile B.class file, and then through A.class to dynamically include B.class.


C,usebean, SetProperty, GetProperty action

<usebean id= "C" class= "Full class name"

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.