|
1、JSP申明語句:
<%! 申明語句 %>
使用申明語句的變量為全局變量,多個用戶執行此JSP頁面,將共享該變量。
如:
復制代碼 代碼如下:
<html>
<head>
<title>JSP Demo</title>
</head>
<body>
<%! int a = 1 ;%>
<%
out.println("a="+a);
a++;
%>
</body>
</html>
結果如下:

再次刷新,會發現如下a的值加一了,因為變量a是全局變量,a++運算多次累計,多用客服端共享:

2、JSP Scriptlets:
<% Java代碼%>
在JSP Scriptlets中可包含多個語句,如:方法,變量, 表達式等;
如:
復制代碼 代碼如下:
<html>
<head>
<title>test demo</title>
</head>
<body>
<%
String str = "Hello,xiaomo!";
out.println(str);
%>
</body>
</html>
結果如下:

3、JSP表達式:
<%= Java代碼 %>
一種簡單的輸出形式,必須有一個可以輸出的值,表達式中的代碼會首先執行,然后轉換成字符串顯示到頁面。
如:
復制代碼 代碼如下:
<html>
<head>
<title>test demo</title>
</head>
<body>
<% String str = "Hello,xiaomo!"; %>
<%= str %>
</body>
</html>
結果如下:

4、JSP注釋:
<!-- --> HTML注釋,會顯示于客戶端;
<%-- --%>JSP注釋,不會顯示于客戶端;
如:
復制代碼 代碼如下:
<html>
<head>
<title>test demo</title>
</head>
<body>
<!-- 這是一個網頁注釋 -->
<%-- 這是一個JSP注釋 --%>
</body>
</html>
結果如下:

大家發現都沒有什么顯示,點擊瀏覽器右鍵查看源碼可以看到網頁注釋是在的,JSP注釋沒有,這個只有在JSP源碼才能看到:
復制代碼 代碼如下:
<html>
<head>
<title>test demo</title>
</head>
<body>
<!-- 這是一個網頁注釋 -->
</body>
</html>
應該很多人都知道,JSP頁面是轉譯成Servlet后在通過服務器轉換成html代碼發送到客戶端顯示的,下面給大家看下本案例的JSP頁面轉換成Servlet后的源碼(其中紅色標注為轉換成前臺的html代碼):
復制代碼 代碼如下:
/*
* Generated by the JASPer component of Apache Tomcat
* Version: Apache Tomcat/7.0.34
* Generated at: 2013-07-03 01:27:37 UTC
* Note: The last modified time of this file was set to
* 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.getDefaultFactory();
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("<html>/r/n");
out.write("/t<head>/r/n");
out.write("/t/t<title>test demo</title>/r/n");
out.write("/t</head>/r/n");
out.write("/t<body>/r/n");
out.write("/t/t<!-- 這是一個網頁注釋 -->/r/n");
out.write("/t/t");
out.write("/r/n");
out.write("/t</body>/r/n");
out.write("</html>");
} 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);
}
}
}
jsp技術:JSP腳本元素和注釋復習總結示例,轉載需保留來源!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。