JSP_2_JSP Basic Syntax

Source: Internet
Author: User

Jsp:java Sever Page, a simplified servlet

Main technology JSP High Security, ASP. PHP Simple and efficient (lamp:linux+apache+mysql+php)

---2-3 jsp page elements constitute a-------------------------------------------------------------------------------------- -

2-3 jsp page Element composition

1 Instructions:

<%@ compiler directive Name Property name = "Property value" ...%>

Page directive: Often at the top of a JSP page, the same page can have multiple page directives

Include directive: Embed an external file into the current JSP file, and the colleague resolves the JSP statement in the page.

Taglib directives: Use the tag library to define new custom labels.

1.1 Page Instruction Syntax:

<%@ page Property 1 = "Property Value" Property 2 = "Property value 1, property value 2" ... Property N= "Property value N"%>

attribute definitions for each instruction:

Language: Declares the type of scripting language used by the current JSP page, usually Java by default.

Import: Imports the package. Default value: None (the following packages are imported by default: java.lang.*; javax.servlet.*; javax.servlet.jsp.*;javax.servlet.http.*;)

ContentType: Used to set the file format and encoding character set of the generated Web page, which is the MIME type and page character set type.

The default MIME type is text/html; The default character set type is Iso-8859-1 (English).

The following are not commonly used

Extends: Specifies the parent class inherited by the Java class that the JSP page compiles from, or the interface that is implemented.

Session: Set whether the JSP page requires an HTTP Session.

Buffer: Specifies the size of the output buffer.

AutoFlush: The content of the output buffer needs to be forced when the output buffer is about to overflow.

Info: Setting the JSP program's information can also be seen as a description, which can be obtained through the Servlet.getservletinfo () method.

ErrorPage: Specifies the error handling page.

Iserrorpage: Sets whether this JSP page is an error handler.

Pageencoding: is the coded character set of the generated Web page.

1<%@ page language= "Java"Import= "java.util.*" contenttype= "text/html; Charset=utf-8 "%>2<%3String Path =Request.getcontextpath ();4String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";5%>6 7<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >89Ten<base href= "<%=basePath%>" > One      A<title>my JSP ' index.jsp ' starting page</title> -<meta http-equiv= "Pragma" content= "No-cache" > -<meta http-equiv= "Cache-control" content= "No-cache" > the<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" > -- + A    at<body> - - -<!--I'm an HTML comment, visible on the client-- -<%--I am a JSP comment that is not visible on the client--%> -<%! inString s = "Zhang San";//A string variable was declared -        intAddintXintY//declares a function that returns an integral type that implements the summation of two integers.  to        { +           returnx+y; -        } the%> *      $<%Panax Notoginseng        //single-line comment -        /*Multi-line comments*/ theOut.println ("Hello, everyone, welcome to learn Java EE development.") "); +%> A<br> theHello, <%=s%><br>. +X+y=<%=add (10,5)%><br> -</body> $

<% @page

[language= "Java"]

[extends= "Package.class"]

[import = "package.class|package.* ..."]

[session= "True|false"]

[buffer = "None|8kb|size Kb"]

[AutoFlush = "True|false"]

[IsThreadSafe = "True|false"]

[info = "text"]

[ErrorPage = "Relativeurl"]

[ContentType = "mimetype[; charset = CharacterSet] "|" text/html; CharSet = iso-8859-1 "]

[pageencoding = "Iso-8859-1"]

[Iserrorpage = "True|false"]

%>

---2-5 jsp comment ---------------------------------------------------------------------------------------

JSP Page Comments:

1.HTML comment:<!--HTML comment-->//client visible

Note for 2.jsp: <%--jsp Comment--%>//Client not visible

3.JSP Script Comment://single line/**/multiline

<%
Single-line Comment
/* Multiline Comment */
Out.println ("Hello, everyone, welcome to learn Java EE development.") ");

%>

---2-7 jsp script ---------------------------------------------------------------------------------------

You can place any executable Java statement in a JSP script, so you can take advantage of the Java language's capabilities, such as connecting to a database and executing a database.

<%jsp Script%>

<%
Out.println ("Hello, everyone, welcome to learn Java EE development.") ");

%>

---2-8 JSP declaration ---------------------------------------------------------------------------------------

JSP declarations are used to declare variables and methods. The syntax format for JSP declarations is as follows:

<%! JSP Declaration%>

<%!
Declaring a variable
public int count = 0;
Declaring a method
Public String info ()
{
Return "Hello:" + count;
}
%>

---2-9 jsp expression ---------------------------------------------------------------------------------------

The expression value output expression executed in the JSP page is converted to the output language inside the servlet.

Syntax format:

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

Example:

<%!
String s = "Zhang San"; A string variable was declared
int add (int x,int y)//declares a function that returns an integral type, implementing the summation of two integers.
{
return x+y;
}
%>

<%
Single-line Comment
/* Multiline Comment */
Out.println ("Hello, everyone, welcome to learn Java EE development.") ");
%>
<br>
Hello, <%=s%><br>.
X+y=<%=add (10,5)%><br>

  

---2-10 jsp page life cycle ------------------------------------------------------------------------------------- --

Apache Work fold Java files generated under App folder (servlet)

How the JSP works:

client→ makes a request →jsp Server (if the JSP page receives the request for the first time, compile the JSP file to generate the corresponding servlet if the JSP page already has a corresponding

← Returns the standard HTML page ← servlet, which dynamically generates standard HTML pages according to the request. Note: HTML documents are dynamically generated on request each time. )

The Jspservice () method is called to process the client's request. One thread is created for each request, and multiple threads are created if there are multiple requests to be processed at the same time

。 Each client request corresponds to a thread. Multi-threaded execution can greatly reduce the resource requirements of the system, and increase the concurrency and response time of the system. But also pay attention to the synchronization problems caused by multithreaded programming, because the servlet is always resident in memory, so it is very fast.

1<%@ page language= "Java"Import= "java.util.*" contenttype= "text/html; Charset=utf-8 "%>2<%@ pageImport= "Java.text.*"%>3<%4String Path =Request.getcontextpath ();5String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";6%>7 8<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >9Ten One<base href= "<%=basePath%>" > A      -<title>my JSP ' index.jsp ' starting page</title> -<meta http-equiv= "Pragma" content= "No-cache" > the<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" > +- A at    -<body> - - -<% -SimpleDateFormat SDF =NewSimpleDateFormat ("yyyy mm month DD Day"); inString s = Sdf.format (NewDate ()); -%> toToday is: <%=s%> +</body> -

---2-12 stage project: Print 99 multiplication table ---------------------------------------------------------------------------------- -----

Print 99 multiplication tables using expression and scripting, respectively

Page directive: Shortcut key "ALT +/" Select the page's properties

"&nbsp;&nbsp;&nbsp;&nbsp;" Output four spaces

"<br>"//Append line wrap label, "<br/>" HTML5 format

1<%@ page language= "Java"Import= "java.util.*" contenttype= "text/html; Charset=utf-8 "%>2<%3String Path =Request.getcontextpath ();4String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";5%>6 7<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >89Ten<base href= "<%=basePath%>" > One      A<title>my JSP ' exercise.jsp ' starting page</title> -      -<meta http-equiv= "Pragma" content= "No-cache" > the<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" > +- A  at -    -<body> -<%! -        //returns the HTML code for the 99 multiplication table, which is called through an expression, and is displayed on the page - String printMultiTable1 () in        { -String s = ""; to            for(inti=1;i<=9;i++) +           { -               for(intj=1;j<=i;j++) the              { *s+=i+ "*" +j+ "=" + (i*j) + "&nbsp;&nbsp;&nbsp;&nbsp;"; $              }Panax Notoginsengs+= "<br>";//Append line Wrapping label -           } the           returns; +        } A         the        //JSP built-in out object, call using script, print 99 multiplication table +        voidPrintMultiTable2 (JspWriter out)throwsException -        { $             for(inti=1;i<=9;i++) $            { -               for(intj=1;j<=i;j++) -              { theOUT.PRINTLN (i+ "*" +j+ "=" + (i*j) + "&nbsp;&nbsp;&nbsp;&nbsp;"); -              }WuyiOut.println ("<br/>");//Append line Wrapping label the           } -        } Wu         -%> About $ -<%=printmultitable1 ()%> -<br> -<% PrintMultiTable2 (out);%> A<br> +      the</body> -

JSP_2_JSP Basic Syntax

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.