Summary examples of JSP script elements and comments

Source: Internet
Author: User
Tags html comment apache tomcat

Today, I reviewed the JSP script elements and comments, and wrote the case. After debugging, the overall summary is as follows,
1. JSP statement:
<%! Statement %>
The variable in the declarative statement is a global variable. When multiple users execute this JSP page, the variable is shared.
For example: Copy codeThe Code is as follows: <Head>
<Title> JSP Demo </title>
</Head>
<Body>
<%! Int a = 1; %>
<%
Out. println ("a =" + );
A ++;
%>
</Body>
</Html>

The result is as follows:
 
Refresh again and you will find that the following a value is added, because variable a is a global variable, and the ++ operation is accumulated multiple times, which is shared by the customer service end:
 
2. JSP Scriptlets:
<% Java code %>
The JSP Scriptlets can contain multiple statements, such as methods, variables, and expressions;
For example:Copy codeThe Code is as follows: <Head>
<Title> test demo </title>
</Head>
<Body>
<%
String str = "Hello, xiaomo! ";
Out. println (str );
%>
</Body>
</Html>

The result is as follows:
 
3. JSP expressions:
<% = Java code %>
In a simple form of output, there must be a value that can be output. The code in the expression is executed first, and then converted to a string to be displayed on the page.
For example:Copy codeThe Code is as follows: <Head>
<Title> test demo </title>
</Head>
<Body>
<% String str = "Hello, xiaomo! "; %>
<% = Str %>
</Body>
</Html>

The result is as follows:
 
4. JSP comments:
<! --> HTML comment, which is displayed on the client;
<% -- %> JSP comment, not displayed on the client;
For example:Copy codeThe Code is as follows: <Head>
<Title> test demo </title>
</Head>
<Body>
<! -- This is a webpage comment -->
<% -- This is a JSP comment -- %>
</Body>
</Html>

The result is as follows:
 
No display is found. Right-click the browser to view the source code. You can see that the webpage comment is in, and the JSP comment is not.JSP source code can be seen: Copy codeThe Code is as follows: <Head>
<Title> test demo </title>
</Head>
<Body>
<! -- This is a webpage comment -->
</Body>
</Html>

Many people know that JSP pages are converted to Servlet and then converted to html code on the server and sent to the client for display, the following shows how to convert the JSP page of this case to the source code of the Servlet (marked in red as the html code converted to the foreground ):Copy codeThe Code is as follows :/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.34
* Generated at: 01:27:37 UTC
* Note: The last modified time of this file was set
* The last modified time of the source file after
* Generation to assist with modification tracking.
*/
Package org. apache. jsp;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import javax. servlet. jsp .*;
Public final class HelloXiaoMo_jsp extends org. apache. jasper. runtime. HttpJspBase
Implements org. apache. jasper. runtime. JspSourceDependent {
Private static final javax. servlet. jsp. JspFactory _ jspxFactory =
Javax. servlet. jsp. JspFactory. getdefafactory Factory ();
Private static java. util. Map <java. lang. String, java. lang. Long> _ jspx_dependants;
Private javax. el. ExpressionFactory _ el_expressionfactory;
Private org. apache. tomcat. InstanceManager _ jsp_instancemanager;
Public java. util. Map <java. lang. String, java. lang. Long> getDependants (){
Return _ jspx_dependants;
}
Public void _ jspInit (){
_ El_expressionfactory = _ jspxFactory. getJspApplicationContext (getServletConfig (). getServletContext (). getExpressionFactory ();
_ Jsp_instancemanager = org. apache. jasper. runtime. InstanceManagerFactory. getInstanceManager (getServletConfig ());
}
Public void _ jspDestroy (){
}
Public void _ jspService (final javax. servlet. http. HttpServletRequest request, final javax. servlet. http. HttpServletResponse response)
Throws java. io. IOException, javax. servlet. ServletException {
Final javax. servlet. jsp. PageContext pageContext;
Javax. servlet. http. HttpSession session = null;
Final javax. servlet. ServletContext application;
Final javax. servlet. ServletConfig config;
Javax. servlet. jsp. JspWriter out = null;
Final java. lang. Object page = this;
Javax. servlet. jsp. JspWriter _ jspx_out = null;
Javax. servlet. jsp. PageContext _ jspx_page_context = null;
Try {
Response. setContentType ("text/html; charset = UTF-8 ");
PageContext = _ jspxFactory. getPageContext (this, request, response,
Null, true, 8192, true );
_ Jspx_page_context = pageContext;
Application = pageContext. getServletContext ();
Config = pageContext. getServletConfig ();
Session = pageContext. getSession ();
Out = pageContext. getOut ();
_ Jspx_out = out;
Out. write ("\ r \ n ");
Out. write ("Out. write ("\ t Out. write ("\ t <title> test demo </title> \ r \ n ");
Out. write ("\ t Out. write ("\ t <body> \ r \ n ");
Out. write ("\ t <! -- This is a webpage comment --> \ r \ n ");
Out. write ("\ t ");
Out. write ("\ r \ n ");
Out. write ("\ t </body> \ r \ n ");
Out. write ("} Catch (java. lang. Throwable t ){
If (! (T instanceof javax. servlet. jsp. SkipPageException )){
Out = _ jspx_out;
If (out! = Null & out. getBufferSize ()! = 0)
Try {out. clearBuffer ();} catch (java. io. IOException e ){}
If (_ jspx_page_context! = Null) _ jspx_page_context.handlePageException (t );
Else throw new ServletException (t );
}
} Finally {
_ JspxFactory. releasePageContext (_ jspx_page_context );
}
}
}

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.