Introduction to 2--JSP syntax and a case study of 9th table on a JSP page

Source: Internet
Author: User
Tags html comment

L JSP template Elements

The HTML content on the JSP page is called a JSP template element. The JSP template element defines the basic skeleton of a webpage, that is, the structure and appearance of the page.

L JSP expression

JSP script expression (expression) is used to output program data to the client. Syntax: <% = variable or expression %>, which is equivalent to output directly with out. print. Example: Current Time: <% = new java. util. Date () %> or out. print (newjava. util. Date ).

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. Variables or expressions in JSP script expressions cannot be followed by semicolons (;). Www.2cto.com

L JSP script snippets

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

<%

Multi-line java code

%>

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.

Java code in JSP script snippets must strictly follow the Java syntax. For example, each execution statement must end with a semicolon. 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:

<%

Int x = 10;

Out. println (x );

%>

<P> This is the JSP page text. </p>

<%

Int y = 20;

Out. println (x );

%>

L The code in multiple script fragments can access each other, as if all the code is placed in a pair of <%>. For example, out. println (x );

L The Java statements in a single script fragment can be incomplete. However, the combined results of multiple script fragments must be complete Java statements, for example:

<%

For (int I = 1; I <5; I ++)

{

%>

<H1> www.it315.org </H1>

<%

}

%>

L 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:

<%!

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! ");

}

%>

L JSP comments

JSP annotation format: <% -- annotation information -- %>. When the JSP Engine translates a JSP page into a Servlet program, it ignores the commented content on the JSP page.

There are three ways to comment:

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

• JSP page comment (hidden comment): A Comment is written in a JSP program but is 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.

<% -- Thisis 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 */

L JSP commands

L JSP tag

L JSP built-in objects

L how to find errors on JSP pages

The following syntaxes will be introduced later in the blog. Today we will introduce these syntaxes.

2. 9-9 Table JSP case

Simple illustration:

 

 
Jiujiubiao. jsp, equivalent to a servlet class

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">

<Html>

<Head>

<Base href = "<% = basePath %>">

<Title> My JSP 'jiujiubiao. 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">

 

</Head>

 

<Body>

<H1> the output result of Table 9 is:

<%

// During type conversion, the parseInt () method of Integer is used. The return value type of this method is int, and its valueOf () method returns an Integer value. An exception is thrown.

 

Int startNumber = Integer. parseInt (request. getParameter ("startNumber "));

Int endNumber = Integer. parseInt (request. getParameter ("endNumber "));

For (int I = startNumber; I <= endNumber; I ++ ){

For (int j = startNumber; j <= I; j ++ ){

Out. print (I + "*" + j + "=" + I * j + "& nbsp ;");

}

Out. print ("<br> ");

}

%>

</Body>

</Html>

Index. jsp; homepage Logon

<% @ 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 'index. 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">

</Head>

 

<Body>

<Center>

<Font size = "20px" color = "blue"> enter two natural numbers to print out the multiplication formula. </font>

<Form action = "./jiujiubiao. jsp" method = "post">

StartNumber

<Input type = "text" name = "startNumber">

<Br>

<Br>

EndNumber

<Input type = "text" name = "endNumber">

<Br>

<Br>

& Nbsp;

<Input type = "submit" value = "submit">

& Nbsp;

<Input type = "reset" value = "reset">

</Form>

</Center>

</Body>

</Html>

 

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.