Mu class network "JAVA met HTML - Chapter JSP JSP basic grammar chapter" study notes

Note: Course Website: https://www.imooc.com/learn/166

2-1: JSP Introduction

Servlet JSP full name of the Java Server Pages, Java server pages, which is simply a simplified design, he achieved using HTML tags in Java. JSP is a dynamic web technology standards, but also JavaEE standard. JSP and Servlet, is executed on the server side.

2-3: JSP Introduction page elements and page instructions

JSP page elements constitute: instruction, expression, small script, statements, comments, static content.

 JSP instructions is three kinds.

(. 1) page command, usually at the top of the JSP page, the same page may have a plurality of page command.

(2) include instructions, an external file into the current JSP file while parsing the JSP page statements.

(3) taglib directive, use of the label to define a new custom tab, enable custom behavior in the JSP page.

page command syntax:

<% @ Page Attribute 1 = "attribute value" Attribute 2 = "1 attribute value, the attribute value 2" ... n-attribute = "attribute value n"%> Notes: @ required between the page and there is a space.

Attributes description Defaults
language Specifies the scripting language used in the JSP page Java
import Scripting language used to refer to the class files through the property no
contentType It is used to specify the file type and the encoding used by JSP pages text/html,ISO-8859-1

 text / html, indicating that it is a text file, and a Web page. The default character set is a plain English character set, ISO-8859-1.

2-5: JSP comments

HTML comments: <-! HTML Comment -> // client visible (can be viewed by the client browser to view source)

JSP comments: <% - JSP Comment -%> // client is not visible

JSP script Note: the use of Java in the JSP script Comment, // a single line comment / * multiline comment * / client is not visible

 

 

2-7: JSP script

JSP scripting elements, is that, Java code in JSP pages that can be performed, we write the code in JSP tag, called JSP script.

grammar:

<% Java Code%>

out.println ( "Hello everyone, welcome to learn JavaEE development."); a built-in objects through the JSP out object to the browser output word.

 

 

 

 

 

 

2-8: JSP statement

JSP declaration element refers to the definition of a variable or method in a JSP page.

grammar:

<%! Java code%>

Behind the percent sign exclamation point!, That this is a declaration statement, followed by some Java code. The Java code can define a variable, it can be defined in a way to facilitate future JSP page script to call a variable or method declared earlier.

<%!

  String s = "John Doe"; // declares a string variable

  int add (int x, int y) {// declare a function

    return x+y;

  }

%>

2-9: JSP expression

JSP expressions: the expression executed in the JSP page.

grammar:

<% = Expression%> // Note: The expression does not end with a semicolon

Hello, <% = s%> <br>

x+y=<%=add(10, 5) %><br>

 

 

 

 

 

 

2-10: JSP page life cycle

This is the focus of this chapter, "JSP syntax": the life cycle of JSP pages.

JSP page lifecycle generally divided into several steps.

First, we assume that the user using the client browser sends a request to the server, for example, said he would visit our Web server project home page index.jsp. We call this the user sends a request to the server requesting access to resources index.jsp this page.

After the server receives the user's request, it must first determine whether the user accessed the resource is the first request.

If this is the first request, then our Tomcat JSP engine to convert the file into a JSP Servlet, a Servlet In essence, he is also a Java class. This Java class will be compiled to generate a byte code file, and then execute this class jspInit () such an initialization method. In particular, note that initialization method, only when the compiled bytecode files corresponding to Perform. That statement throughout this period, he performed only once.

If the request is not the first time, then he would go directly to the access byte code file generated.

Then to resolve the implementation of a class which is called jspService () this method. the jspService () method is used to process user requests.

explain in detail:

the jspService () method is called to handle client requests. For each request, JSP engine creates a new thread to handle the request. If there are multiple clients simultaneously request the JSP file, the JSP engine creates multiple threads. It is a multi-threaded approach based. Each client request corresponding to a thread. Multi-threaded execution can significantly reduce the demand on system resources, and improve concurrency and system response time. But also pay attention to synchronization problems caused by multi-threaded programming, sharing, and protection of critical resources such as some will be triggered. Since the Servlet always stationed in memory, the response is very fast.

Examples of learning:

New Project JspLifeCycleDemo, and New index1.jsp. And added to the tomcat server, start the server.

Double-click the ctrl key, use the search listary software index1_jsp. (If the compiled byte code file, the name will be index1_jsp.java and index1_jsp.class)

 

 No. The reason is that the server has not received a request of a user, i.e., never visited. There will be no JSP pages index1.jsp Servlet compiled bytecode files generated.

 Access index1.jsp resources JspLifeCycleDemo project:

 

 Again entire computer search index1_jsp, you can see, is compiled into bytecode files:

 

 

 

 

 

 

 tomcat directory structure, which is very important directory is work, which is stored compiled bytecode files.

Open index1_jsp.java.

 

 

There are _jspInit (), initialization method, only when the first jsp pages is requested before execution.

Then, servlet resident in memory. Then for each user request, and then create a thread. It is a method of execution by each thread is called again _jspService () of. For users, that each user's request, went to call _jspService () method to handle the user's request.

The above is the life cycle of JSP pages.

When the JSP page content changes, JSP engine need to recompile the page.

Examples of verification:

 

Modify index1.jsp:

 

 refresh page:

 

 

Then you can see, modified date index1_jsp.java and index1_jsp.class two documents into a time now.

 

 Description This class is recompiled before. Open index1_jsp.java, we can see which contains the latest code:

 

 

2-11: Phase Project

Expressions and scripts were used to achieve print multiplication table.

 

 

 

 

We need to throw an exception:

 

 

 

 success.

 

Guess you like

Origin www.cnblogs.com/pingfanliliang/p/11664070.html