JSP syntax _ Shi guoxu's personal summary

Source: Internet
Author: User

JSP template Elements
1. the HTML content on the JSP page is called a JSP template element.

2. the JSP template element defines the basic skeleton of a webpage, that is, the structure and appearance of the page.
JSP script expression
1. JSP script expression (expression) is used to output program data to the client.

Syntax: <% = variable or expression %>

Example: Current Time: <% = new java. util. Date () %>

2. When translating a script expression, the JSP Engine converts the program data into a string and uses out. print (...) Data is lost to the client.

3. variables or expressions in JSP script expressions cannot be followed by semicolons (;)
JSP script snippets (1)

1. the JSP script snippet (scriptlet) is used to write multiple lines of Java code on the JSP page. Syntax:

<%
Multi-line java code
%>

2. note: Only java code can appear in JSP script fragments, and no other template elements can appear. The JSP Engine translates JSP pages, the Java code in the JSP script segment will be stored in the Servlet's _ jspService method.

3. the Java code in JSP script snippets must strictly follow the Java syntax. For example, each execution statement must end with a semicolon.
JSP script snippets (2)

1. a JSP page can contain multiple script fragments. Text, HTML tags, and other JSP elements can be embedded between two or more script fragments.

Example:
[Html]
<%
Int x = 10;
Out. println (x );
%>
<P> This is the JSP page text. </p>
<%
Int y = 20;
Out. println (x );
%>


2. The code in multiple script fragments can be accessed from each other, as if all the code is placed in a pair of <%>. For example, out. println (x );
3. the Java statements in a single script fragment can be incomplete. However, the result after the combination of multiple script fragments must be a complete Java statement, for example:

[Html] view plaincopy
<%
For (int I = 1; I <5; I ++)
{
%>
 
 
<H1> www.it315.org </H1>
 
 
<%
}
%>


JSP Declaration
All code written on the JSP page is translated to the servlet service method by default, and the java code in the Jsp declaration is translated to the _ jspService method. Syntax:
<%!
Java code
%>

Therefore, JSP declarations can be used to define static code blocks, member variables, and methods of Servlet programs converted from JSP pages.

Multiple Static code blocks, variables, and functions can be defined in one JSP declaration, or they can be separately defined in multiple JSP declarations.


The scope of JSP implicit objects is limited to Servlet's _ jspService method. Therefore, these implicit objects cannot be used in JSP declarations.
Case

[Html]
<%!
Static
{
System. out. println ("loading Servlet! ");
}
Private int globalVar = 0;
Public void jspInit ()
{
System. out. println ("initializing jsp! ");
}
%>
<%!
Public void jspDestroy ()
{
System. out. println ("destroying jsp! ");
}
%>


JSP comments
JSP annotation format:
<% -- Comment -- %>

When the JSP Engine translates a JSP page into a Servlet program, it ignores the commented content on the JSP page.

Note

There are three ways to comment

HTML comments (output comments): the comments can be seen when the client views the source code. For example,
<! -- This is an html comment. it will show up int the response. -->

JSP page comment (hidden comment): The comment is written in the JSP program but not sent to the customer. Therefore, the Comment cannot be seen when the client views the source code. Such annotations are ignored during JSP compilation.
<% -- This is a JSP comment. it will only be seen in jsp code -- %>

Java Note: it can only appear in the Java code area and cannot appear directly on the page. // Single line comment/* multi-line comment */
Instance

Compile the index1.jsp page. Two input text boxes are provided on the page to collect the data submitted by the user. The "Submit" and "cancel" buttons are provided to help you submit or cancel the corresponding operations.
Compile the result1.jsp page and display the multiplication table based on the number submitted by the user.



Case source code:

// Index1.jsp

[Html]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
 
 
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basePath %>">
 
<Title> My JSP 'index1. jsp 'starting page </title>
 
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<! --
<Link rel = "stylesheet" type = "text/css" href = "styles.css">
-->
 
 
</Head>
 
<Body background = "images/tupian.jpg">
<Center>
<P align = "center"> enter two natural numbers to print the multiplication table. </p>
<Form action = "jiujiu. jsp" method = "post">
<Table border = "2px" cellpadding = "0" cellspacing = "0">
<Tr>
<Td> startNumber: </td>
<Td> <input type = "text" name = "startNumber"/> </td>
</Tr>
<Tr>
<Td> endNumber: </td>
<Td> <input type = "text" name = "endNumber"/> </td>
</Tr>
<Tr>
<Td> <center> <input type = "submit" value = "submit"> </td>
<Td> <center> <input type = "reset" value = "reset"/> </td>
</Tr>
 
 
</Table>
 
 
</Form>
</Center>
</Body>
</Html>


// Result1.jsp

[Html]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
 
 
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basePath %>">
 
<Title> My JSP 'jiujiu. jsp 'starting page </title>
 
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<! --
<Link rel = "stylesheet" type = "text/css" href = "styles.css">
-->
 
 
</Head>
 
<Body>
<Font size = 12 color = red> This is the multiplication table you want !! </Font>
<Br>
<%
// Int startNum = Integer. valueOf (request. getParameter ("startNumber "));
Int startNum = Integer. parseInt (request. getParameter ("startNumber "));
// Int endNum = Integer. valueOf (request. getParameter ("endNumber "));
Int endNum = Integer. parseInt (request. getParameter ("endNumber "));
For (int I = startNum; I <= endNum; I ++)
{
For (int j = 1; j <= I; j ++) www.2cto.com
{
Out. print (j + "*" + I + "=" + (I * j) + "\ t ");
Out. print ("");
}
Out. print ("<br/> ");
}
%>
</Body>
</Html>


Too many other users

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.