欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-29 20:37:10
今天复习了jsp脚本元素和注释部分,案例写出来,大家自己调试下,整体总结如下, 1、jsp申明语句: <%! 申明语句 %> 使用申明语句的变量为全局变量,多个...
今天复习了jsp脚本元素和注释部分,案例写出来,大家自己调试下,整体总结如下,
1、jsp申明语句:
<%! 申明语句 %>
使用申明语句的变量为全局变量,多个用户执行此jsp页面,将共享该变量。
如:
复制代码 代码如下:

<html>
<head>
<title>jsp demo</title>
</head>
<body>
<%! int a = 1 ;%>
<%
out.println("a="+a);
a++;
%>
</body>
</html>

结果如下:
JSP脚本元素和注释复习总结示例 
再次刷新,会发现如下a的值加一了,因为变量a是全局变量,a++运算多次累计,多用客服端共享:
JSP脚本元素和注释复习总结示例 
2、jsp scriptlets:
<% java代码%>
在jsp scriptlets中可包含多个语句,如:方法,变量, 表达式等;
如:
复制代码 代码如下:

<html>
<head>
<title>test demo</title>
</head>
<body>
<%
string str = "hello,xiaomo!";
out.println(str);
%>
</body>
</html>

结果如下:
JSP脚本元素和注释复习总结示例 
3、jsp表达式:
<%= java代码 %>
一种简单的输出形式,必须有一个可以输出的值,表达式中的代码会首先执行,然后转换成字符串显示到页面。
如:
复制代码 代码如下:

<html>
<head>
<title>test demo</title>
</head>
<body>
<% string str = "hello,xiaomo!"; %>
<%= str %>
</body>
</html>

结果如下:
JSP脚本元素和注释复习总结示例 
4、jsp注释:
<!-- --> html注释,会显示于客户端;
<%-- --%>jsp注释,不会显示于客户端;
如:
复制代码 代码如下:

<html>
<head>
<title>test demo</title>
</head>
<body>
<!-- 这是一个网页注释 -->
<%-- 这是一个jsp注释 --%>
</body>
</html>

结果如下:
JSP脚本元素和注释复习总结示例 
大家发现都没有什么显示,点击浏览器右键查看源码可以看到网页注释是在的,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);
}
}
}