Summary of JSP basic syntax

Source: Internet
Author: User
I. jsp operating principle:

When a browser requests a JSP page from the Web application server, the Web Application Server converts it into a Servlet File (that is,. java file), and then compile the Java file into a bytecode file (. class file). Finally, the Web Application Server loads the converted servlet instance, processes client requests, and returns an HTML response to the client browser.

 

 

Ii. Composition of JSP pages:

The JSP page consists of command identifiers, HTML code, embedded Java Script programs, and comments. The Java script program is embedded into the HTML page through <% and %>.

 

 

Iii. Three types of script elements on the JSP page: expression format, small script format, and Declaration format
    • Expression format:This interface is used to directly call Java expressions on the page to obtain the returned values. Its basic syntax is as follows: <% =Java expressions/variables%>

 

    • Script format:Use <% and %> in HTML to embed the Java script program and perform corresponding logic processing. The basic syntax is as follows: <%... Arbitrary Java code...%>
    • JSPDeclaration format: used to define the variables or methods used in the Java script language. The basic syntax is as follows: <%!... Java declaration...%>
4. Three instruction elements of JSP:

Contains page, include, and taglib3 command elements. The definition method is: <%Command name[... One or more command attributes...]
%>

 

    • Page command page:It is used to set global attributes in JSP files, such as JSP page encoding, content type, referenced class library, etc. Example:

<% @ Page import = "Java. text. simpledateformat "%> <br/> <% @ page Language =" Java "contenttype =" text/html; charset = UTF-8 "<br/> pageencoding =" UTF-8 "%> <br/> 

Note::PageencodingAttributes andContenttypeAttribute differences


Usually both exist at the same time, pageencoding attribute is to specify the JSP page encoding format, if its value is not UTF-8, the Chinese on the page will be garbled; the contenttype attribute specifies the content type that the server responds to the browser after a request is sent. If its value is set to "text/html; charset = UTF-8 ", the page that the server responds to is an HTML document or text content whose character set is a UTF-8 that supports Chinese.

    • File Inclusion command include:The include command is used to reference files outside the page. a jsp page can be included in another JSP page to realize JSP page reuse. The syntax of the command is: <% @ include
      File ="Path"%>; Path indicates the path of the referenced file.

 

Note::The include command is used to generate a file. Therefore, there cannot be variables with the same name in the included and contained files; otherwise, a compilation error occurs.

 

Tips:Generally, the header of a website page (banner) is the same as that at the bottom of the page. You can create these two pages separately and include them in other pages using the include command, this reduces code redundancy.


    • REFERENCE The taglib command of the tag Library:Used to reference a tag library or custom tags. The taglib command can be used to tell the container which tag library will be used on this JSP page, and a prefix can be specified for the referenced tag library. Using the tag library in JSP pages can simplify JSP pages and reduce the use of Java code. Common tag libraries include jstl, sturs, and spring labels. Syntax of the taglib command: <% @ taglib
      Prefix ="Tagprefix"Uri = "tagurl" %>; Prefix: Specify the prefix of the tag Library; uri = specify the location of the tag library file;

 

Example: Use the taglib command to reference the standard tag Library (jstl)

<% @ Taglib prefix = "SQL" uri = "http://java.sun.com/jsp/jstl/ SQL" %> <br/> <SQL: query Var = "book"> <br/> select * From books <br/> </SQL: Query> <br/> 

 

5. jsp standard action elements:

The format of JSP standard action elements is as follows: <JSP:Tag Name>, Which is expressed in strict xml tag syntax. These JSP tag action elements are executed in the user request phase (one of the three stages of JSP execution ). These standard action elements are embedded in JSP files and can be directly used without reference.

Based on the functions of each standard action, you can divide them into six groups:

  1. The standard action to Use Javabean in JSP: <JSP: usebean> defines a JSP page to use a JavaBean instance, and the ID attribute defines an Instance name. <JSP: getproperty> obtains an attribute value from a JavaBean and adds it to the response. <JSP: setproperty> sets the attribute value in a JavaBean;
  2. Standard action to include other JSP files or Web Resources in JSP: <JSP: Include> the response from a servlet or JSP file is included in the request processing stage. Note that it is different from the include command;
  3. Forward the received request to another JSP page or web resource for further operations. Standard action: <JSP: Forward> forward the processing of a request to another servlet or JSP page;
  4. Specify the standard action of the parameter in the middle of other standard actions: <JSP: param> for the use of <JSP: Include> or <JSP: forward> Add a pass parameter value for the request passed to another servlet or JSP page;
  5. The standard action of embedding a Java object (such as an applet, a small Java program running on the client) on the client page: <JSP: plugin> Generate the object or embed tag for the Java Plug-in based on the browser type; <JSP: Params>, <JSP: fallback>;
  6. Standard actions for marking files in English only: <JSP: attribute>, <JSP: Body>, <JSP: invoke>, <JSP: Dobody>, <JSP: Element>, <JSP: Text>, <JSP: output>.

 

 

Brief descriptions of common standard actions:

  1. <JSP: usebean>: defines that the JSP page uses a JavaBean instance;
  2. <JSP: setproperty>: Set the attribute value in a JavaBean;
  3. <JSP: getproperty>: gets the attribute value of a JavaBean;
  4. <JSP: Include>: contains an external file on the JSP page;
  5. <JSP: Forward>: Transfers incoming requests to another page for processing;
  6. <JSP: param>: used to pass parameter values;
  7. <JSP: plugin>: used to specify the attributes of the plug-in inserted in the client browser;
  8. <JSP: Params>: used to pass parameter values to the plug-in on the HTML page;
  9. <JSP: fallback>: Specifies how to handle situations where the customer does not support plug-ins;

Supplement:JSPExecuted3Stages

  1. Translated: compiled JSP files are first converted into Java source code by the JSP engine in the Web Container (on the server side;
  2. Compilation phase: Java source code is compiled into executable bytecode in binary format;
  3. Request Phase: When client requests are easily accepted, execute the JSP file that has previously compiled and planned the binary bytecode. After the request is processed, the container then sends the generated page to the client for display.

 

Difference: UnderstandingIncludeCommands and<JSP: Include>Essential differences of action representation

  1. The include command specifies the file to be included through the file attribute, and the file attribute does not support expressions. The <JSP: Include> action identifier specifies the file to be included through the page attribute, the page attribute supports JSP expressions;
  2. When the include command is used, the content of the contained file will be inserted to the include page intact, and then the JSP compiler will compile the merged file into a Java file; when the <JSP: Include> action identifier is used for execution, the program forwards the request to the contained page and outputs the execution result to the browser, then return the containing page to continue executing the code.
  3. When the include command is used to include a file, because the contained file will eventually generate a file, there cannot be duplicate variables or methods in the included file or included file; when you use the <JSP: Include> action to identify the file to be included, because each file is compiled separately, the variables and methods in the file to be included do not conflict with those in the file.

 

 

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.