jsp

位置:IT落伍者 >> jsp >> 浏览文章

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


发布日期:2022年05月14日
 
JSP脚本元素和注释复习总结示例

今天复习了JSP脚本元素和注释部分案例写出来大家自己调试下整体总结如下
JSP申明语句
<%! 申明语句 %>
使用申明语句的变量为全局变量多个用户执行此JSP页面将共享该变量

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


结果如下

再次刷新会发现如下a的值加一了因为变量a是全局变量a++运算多次累计多用客服端共享

JSP Scriptlets
<% Java代码%>
在JSP Scriptlets中可包含多个语句方法变量 表达式等

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


结果如下

JSP表达式
<%= Java代码 %>
一种简单的输出形式必须有一个可以输出的值表达式中的代码会首先执行然后转换成字符串显示到页面

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


结果如下

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/
* 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);
}
}
}

上一篇:不用迭代算法而快速实现的jsp树结构

下一篇:JSP中pluginAction的使用