|
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脚本元素和注释复习总结示例,转载需保留来源!
郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。