JSP脚本元素和注释复习总结示例

时间:2021-05-26

今天复习了JSP脚本元素和注释部分,案例写出来,大家自己调试下,整体总结如下,
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);
}
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章