Introduction to JSP knowledge and jsp knowledge
Hide comments
Written in the JSP program, but not sent to the customer.
JSP syntax
<% -- Comment -- %>
Example:
Copy codeThe Code is as follows:
<% @ Page language = "java" %>
<Html>
<Head> <title> A Comment Test </title> <Body>
<H2> A Test of Comments <% -- This comment will not be visible in the page source -- %>
</Body>
</Html>
Description
Characters marked with hidden comments are ignored during JSP compilation. This annotation is useful when you want to hide or annotate your JSP program. The JSP compiler does not compile the statements between <% -- and -- %>. It is not displayed in the client's browser or in the source code.
You can write comments between <% -- %>, but cannot use "-- %> ".
========================================================== ======================================
Statement
Declare valid variables and methods in JSP programs
JSP syntax
<%! Declaration; [declaration;] +... %>
Example
Copy codeThe Code is as follows:
<%! Int I = 0; %>
<%! Int a, B, c; %>
<%! Circle a = new Circle (2.0); %>
Description
Declare the variables and methods you will use in the JSP program. You must do the same. Otherwise, an error will occur.
You can declare multiple variables and methods at a time, as long as they end with ";". Of course, these declarations are valid in Java.
When declaring methods or variables, pay attention to the following rules:
The declaration must end with ";" (Scriptlet has the same rule, but the expression is different ).
You can directly use declared variables and methods included in <% @ page %> without declaring them again.
A statement is valid only on one page. If you want to use some declarations for each page, you 'd better write them into a separate file and include them with the <% @ include %> or <jsp: include> element.
========================================================== ======================================
Expression
Contains an expression that complies with the JSP syntax.
JSP syntax
<% = Expression %>
Example
Copy codeThe Code is as follows:
<Font color = "blue"> <% = map. size () %> </font>
<B> <% = numguess. getHint () %> </B>.
Description
The expression element represents an expression defined in the script language. It is automatically converted to a string after running, and then inserted into the expression for display at the location of the JSP file. Because the value of this expression has been converted into a string, you can insert this expression in a line of text (in the same form as ASP ).
When using expressions in JSP, remember the following:
You cannot use a semicolon (";") as the expression Terminator. But the same expression must end with a semicolon when used in scriptlet! View Scriptlet
This expression element can include any expressions that are valid in Java Language Specification.
Sometimes expressions can also be used as attribute values of other JSP elements. An expression can become very complex and may consist of one or more expressions. The order of these expressions is from left to right.
A simple JSP Programming
I wrote it for you!
Inputsting. jsp:
<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %>
<Html>
<Head>
<Title> inputsting. jsp </title>
</Head>
<Body>
<! -- Form start, submit to computer. jsp -->
<Form name = "form1" method = "post" action = "computer. jsp">
<Label>
<Input type = "text" name = "text">
</Label>
<Label>
<Input type = "submit" name = "Submit" value = "submit">
</Label>
</Form>
<! -- Form introduction -->
</Body>
</Html>
Computer. jsp:
<% @ Page contentType = "text/html; charset = gb2312" language = "java" import = "java. SQL. *" errorPage = "" %>
<%
String s = request. getParameter ("text"); // get the text attribute value of the submitted Form and put it in s, that is, inputsting. the text content in the form of jsp is assigned to s,
Int I = s. length (); // calculate the length of s, that is, the length of text in the form of inputsting. jsp!
%>
<Html>
<Head>
<Title> computer. jsp </title>
</Head>
<% Out. println ("submitted String Length:" + I); %>
<Body>
</Body>
</Html>
Basic jsp knowledge
<% @ %> This is a directive element that specifies page information. We recommend that you import a JSTL library. If you want to use an EL function fn: escapeXml (), declare <% @ taglib prefix = "fn" uri = "java.sun.com/jsp/jstl/core" %>
<%! %>, <%> Is used to add small pieces of code on the JSP page, such as Java code
<%> The entire code block can be added to the page.
<%! %> This is used for declaration. This is not recommended.
Another <% = %> is used to add an expression and obtain the result. For example, <% = 1 + 1%>