Java EE JSP Programming Basics

Source: Internet
Author: User

First, JSP programming introduction

JSP is a common static HTML and Dynamic HTML mixed coding technology, can be said to be a variant of the servlet, compared to the servlet it is more like a normal web page. The JSP will take a long time to run for the first time, in the nature of the servlet, which means that the JSP will be automatically converted by the server Tomcat to the servlet after the load runs.

In addition to ordinary HTML code, the JSP page contains three other main components: script elements, instructions, and actions. Script elements are used to embed Java code in a JSP page, and directives are used to control the structure of the servlet as a whole, and actions are used to introduce existing components to control the behavior of the Web container.

Second, JSP script element (1), Output expression

Syntax: <%= expression%>

Effect: The result of an output expression

Note: All variables in the expression must be variables that have already been declared, there can be no spaces between the% and = numbers, and no semicolon is required after the expression

Example:

1 <%for (int i = 1;i < 7; i++) {%>2     

3 <%}%>

(2), notes

Syntax:<!--annotation--with <%--comment--%> two kinds

Function: The former is the output to the client's comment, the latter does not output to the client, only the JSP comments, at run time such comments will not be translated into HTML comments that are not visible when the client view the source code.

Example:

1 <!--client can see the annotations--2 <%--The client cannot see the comment--%>
(3), declaring variables, methods, and classes ①, declaring variables

Syntax: <%! declaration Code%>

Precautions:

The variables declared in 1.JSP are scoped to the entire page, so as long as the variables can be declared and used in the page, it is customary for us to declare and use variables in the first order.

Variables declared in 2.JSP are global variables on the server. We all know that the JSP runtime is automatically converted to a servlet, and that there is only one instance of the server container within each servlet runtime, and different thread responses are generated for different client requests. Therefore, any modification of the variables declared in the JSP will affect all clients accessing the JSP page.

Example:

1 <%--jsp variable declaration--%>2 3 <%i++; %>4 <p> You are the first <%=i%> to visit this site!</p>5 <!--JSP declares that the variable is scoped to the entire page, so the declaration statement can be placed in the back .--> 6 <%! int
②, declaring methods

Syntax: <%! declaration Code%>

Note: The methods declared in the JSP page are valid throughout the page, but the variables defined in the method are only valid in the method.

Example:

1 <%--jsp method declaration--%>2 3 <%! String SayHello () {4     return "Hello"; 5     } %>6 <%=sayhello ()%>
③, declaring class

Syntax: <%! declaration Code%>

Note: The classes declared in the JSP page are valid throughout the page.

Example:

1<%--jsp class declaration--%>23<%! Public classsayhello{4     BooleanCounty;5SayHello (BooleanCounty) {6          This. county=County;7     }8 String Hello () {9         if(county)Ten             return"Hello"; One         Else A             return"Hi"; -     } -}%> the<%sayhello Shello =NewSayHello (false); %> -<%=shello.hello ()%>
Third, JSP directive (a), page directive

The page directive is used to define global properties for the entire JSP page. Here are some common properties:

1.language Properties

Syntax: <% @page language= "Language"%>

Description: Used to indicate the programming language that the JSP script uses, which is the file compilation language. Currently, the attribute value for this property is only "Java".

2.contentType Properties

Syntax: <% @page contenttype= "Language"%>

Description: Specify how MIME types and JSPS are encoded

3.pageEncoding Properties

Syntax: <% @page pageencoding= "Language"%>

Description: Specify the Encoding method

Usage examples:

1 <% @page language= "java" contenttype= "Text/html;charset=utf-8"2     pageencoding= "UTF-8"% > <%--1. Specify the programming language that the JSP page script uses, that is, set the compilation language 3                                2. Specifies the type of mime and the character encoding of the JSP file 4                                3. Specify how the JSP file itself is encoded--%>

4.import Properties

Syntax: <% @page import= "java package"%>

Description: Import a Java package, you can import only one or more, import multiple Java packages when the middle separated by commas.

Usage examples:

1 Import= "Javax.websocket.Session"%>2Import= "java.util.*,java.lang.*"%> <%-- Import Java Package--%>

5.session Properties

Syntax: <% @page session= "true"%> or <% @page session= "false"%>

Description: Sets whether the JSP page supports sessions, and by default allows session values to be true.

1 <% @page session= "false"%> <%--indicates whether the JSP page supports sessions (default support)--%>

6.errorPage Properties

Syntax: <% @page errorpage= "program"%>

Description: Specifies the program that is used to process a JSP page program when an error occurs.

Usage examples:

1 <% @page errorpage= "error.jsp"%> <%--handled by error.jsp when error occurs--%>

7.isThreadSafe Properties

Syntax: <% @page isthreadsafe= "true"%> or <% @page isthreadsafe= "false"%>

Description: Sets whether the JSP file can handle requests from multiple users at the same time, by default a JSP can handle multiple processes, that is, the session value is true.

Usage examples:

1 <% @page isthreadsafe= "false"%> <%--only allow single thread, default is true to allow multithreading--%> by default
(ii), include directives

Description: The include directive embeds a file within a JSP page, and the file can be an HTML file, a JSP file, or another text file.

Usage examples:

1 
(iii), taglib Directive Iv. JSP action

The JSP action component is a markup in XML syntax format that is used to control the behavior of the Web container. The JSP action component dynamically inserts files into the page, reuses the JavaBean component, redirects the user to another page, and so on.

There are several common JSP action components:

(i), Include action components

Syntax: <jsp:include page= "file name" flush= "true"/>

Description: The flush parameter must be true to not use false.

Caveats: The Include action component and the include directive element are very different.

The Include action component does not process the files it contains until it is executed, so the JSP page and the contained file are both logically and physically independent, and if the file is modified, the result of the file modification can be seen at run time.

Files included in the include directive must re-translate the JSP page into a Java file in order to see the modified content, or only the contents of the file before it is modified.

(ii), Forward action components

Syntax: <jsp:forword page= "page name"/>

Description: The Web page displayed by the browser to another HTML or JSP Web page, the client sees the address is the address of page A, but the actual content is the content of the B page.

Note: There can be no content output to the client before using the forward component, or there will be an unexpected throw. The code after the <jsp:forword> tag is no longer executed.

(iii), PARAM action Components

Syntax: <jsp:param name= "parameter name" value= "value"/>

Description: Used to pass parameter information and must be used in conjunction with the Include and forward action components.

(iv), plugin action components

Syntax: <jsp:plugin type= "Bean|applet" code= "Classfilename" codevase= "Classfiledirectoryname" >

Description: Executes an applet or bean

(v), SetProperty, GetProperty and Usebean action components (here is a brief description of the JavaBean after the detailed record)

Setproperty:<jsp:setproperty> Setting properties for JavaBean

Getproperty:<jsp:getproperty> output the properties of a JavaBean

Usebean:<jsp:usebean> Find or instantiate a JavaBean

Java EE JSP Programming Basics

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.