熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

JSP腳本元素和注釋復習總結示例

2013-11-15 11:58:54  來源: JSP教程 

  今天復習了JSP腳本元素和注釋部分案例寫出來大家自己調試下整體總結如下
JSP申明語句
<%! 申明語句 %>
使用申明語句的變量為全局變量多個用戶執行此JSP頁面將共享該變量

復制代碼 代碼如下:
<html>
<head>
<title>JSP Demo</title>
</head>
<body>
<%! int a = ;%>
<%
outprintln("a="+a);
a++;
%>
</body>
</html>

  
結果如下
01.gif 
再次刷新會發現如下a的值加一了因為變量a是全局變量a++運算多次累計多用客服端共享
02.gif 
JSP Scriptlets
<% Java代碼%>
在JSP Scriptlets中可包含多個語句方法變量 表達式等

復制代碼 代碼如下:
<html>
<head>
<title>test demo</title>
</head>
<body>
<%
String str = "Helloxiaomo!";
outprintln(str);
%>
</body>
</html>

  
結果如下
03.gif 
JSP表達式
<%= Java代碼 %>
一種簡單的輸出形式必須有一個可以輸出的值表達式中的代碼會首先執行然後轉換成字符串顯示到頁面

復制代碼 代碼如下:
<html>
<head>
<title>test demo</title>
</head>
<body>
<% String str = "Helloxiaomo!"; %>
<%= str %>
</body>
</html>

  
結果如下
04.gif 
JSP注釋
<! > HTML注釋會顯示於客戶端
<% %>JSP注釋不會顯示於客戶端

復制代碼 代碼如下:
<html>
<head>
<title>test demo</title>
</head>
<body>
<! 這是一個網頁注釋 >
<% 這是一個JSP注釋 %>
</body>
</html>

  
結果如下
05.gif 
大家發現都沒有什麼顯示點擊浏覽器右鍵查看源碼可以看到網頁注釋是在的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/
* Generated at: :: 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 orgapachejsp;
import javaxservlet*;
import javaxservlet*;
import javaxservletjsp*;
public final class HelloXiaoMo_jsp extends orgapachejasperruntimeHttpJspBase
implements orgapachejasperruntimeJspSourceDependent {
private static final javaxservletjspJspFactory _jspxFactory =
javaxservletjspJspFactorygetDefaultFactory();
private static javautilMap<javalangStringjavalangLong> _jspx_dependants;
private javaxelExpressionFactory _el_expressionfactory;
private orgapachetomcatInstanceManager _jsp_instancemanager;
public javautilMap<javalangStringjavalangLong> getDependants() {
return _jspx_dependants;
}
public void _jspInit() {
_el_expressionfactory = _jspxFactorygetJspApplicationContext(getServletConfig()getServletContext())getExpressionFactory();
_jsp_instancemanager = orgapachejasperruntimeInstanceManagerFactorygetInstanceManager(getServletConfig());
}
public void _jspDestroy() {
}
public void _jspService(final javaxservlet final javaxservlet
throws javaioIOException javaxservletServletException {
final javaxservletjspPageContext pageContext;
javaxservlet
final javaxservletServletContext application;
final javaxservletServletConfig config;
javaxservletjspJspWriter out = null;
final javalangObject page = this;
javaxservletjspJspWriter _jspx_out = null;
javaxservletjspPageContext _jspx_page_context = null;
try {
responsesetContentType("text/html;charset=utf");
pageContext = _jspxFactorygetPageContext(this request response
null true true);
_jspx_page_context = pageContext;
application = pageContextgetServletContext();
config = pageContextgetServletConfig();
session = pageContextgetSession();
out = pageContextgetOut();
_jspx_out = out;
outwrite("rn");
outwrite("<html>rn");
outwrite("t<head>rn");
outwrite("tt<title>test demo</title>rn");
outwrite("t</head>rn");
outwrite("t<body>rn");
outwrite("tt<! 這是一個網頁注釋 >rn");
outwrite("tt");
outwrite("rn");
outwrite("t</body>rn");
outwrite("</html>");
} catch (javalangThrowable t) {
if (!(t instanceof javaxservletjspSkipPageException)){
out = _jspx_out;
if (out != null && outgetBufferSize() != )
try { outclearBuffer(); } catch (javaioIOException e) {}
if (_jspx_page_context != null) _jspx_page_contexthandlePageException(t);
else throw new ServletException(t);
}
} finally {
_jspxFactoryreleasePageContext(_jspx_page_context);
}
}
}


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20021.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.