What is JSP
JSP belongs to the Dynamic Web page, for example, when searching for information on the Internet, the program makes a query database operation, which is the function of a dynamic Web page. HTML does not have the ability to query the database, but Java code can access the database through JDBC technology. Therefore, you can mix Java code in the middle of the HTML code, so that the Web page has the ability to access the database. A Web page embedded with Java code is a JSP.
Writing JSPs
Among them, out is one of the nine internal objects of the JSP, and the internal objects are automatically instantiated when the page is loaded into the server. Its role is to output content to the page and manage the server buffers.
Test JSP
You first need to open the server and then deploy the project to the server. Finally enter the URL to test, a total of three ways:
1. http://localhost:8080/Project name/file name. jsp
2. http://127.0.0.1:8080/project name/file name. jsp
3, http://local ip:8080/project name/file name. jsp
In MyEclipse this editor, the JSP page is generally modified to not need to redeploy the project and restart the server, only need to refresh. In the browser is not able to view the JSP file Java code, because the Java code is stored in the server, real-time generation of HTML pages, unlike HTML is written in advance by the user on the server, the Web server sent to the client, so that JSP is a server-side dynamic page.
The difference between Out.print and out.println
The former outputs the source code to the client, does not break the line, and the latter wraps. It is important to note that the latter is a newline in the client HTML source code and not a newline in the page display. You need to use <BR> to wrap lines on the page, such as:
JSP expressions
The role of the JSP expression is to define some of the output of the JSP, the basic syntax is:
<% = variable/return value/Expression%>
Using JSP expressions
The effect of using an expression is to output content to the client, such as:
JSP program segment
In a JSP, an expression can only appear in a single line, and only the results of its operations are output to the client. If you need to output data in the JSP program, but also to implement a series of complex logical operations such as defining variables, the expression can not meet the requirements, the need for JSP program segment. In fact, the JSP program segment is the Java code snippet inserted into the JSP program.
Writing JSP program segments
Mixing JSP program segments, HTML, and expressions
JSP can be placed in HTML, you can also put in JSP program segments and JSP expressions, they can be flexibly mixed use.
scope of variables in a program segment
In the JSP program segment, the definition and use of variables are sequential, as the following code will error:
In the above example, the variable name and age are used first, and then they are defined. A 500 error occurred running, meaning that the program cannot compile, and the compilation fails. Therefore the variable msg is not allowed to be used before it is defined.
What is a JSP declaration
A JSP declaration can define global variables in a Web page that can be used anywhere in a JSP page. In practical applications, methods, page global variables, and even class declarations can be placed in the JSP Declarations section. Previously learned that the variables defined in the JSP program segment can only be declared and used, and the variables defined in the JSP declaration are web-level, and the system will take precedence. This means that using JSP declarations you can define variables anywhere in the JSP.
Writing JSP declarations
You can also define methods in the JSP declaration, such as:
JSP claims issues to be aware of
The JSP declaration can only be defined, but control logic cannot be implemented, such as the inability to use Out.print for output operations:
JSP basic Syntax (ii)