JSP脚本元素和注释复习总结示例
网络编程 2021-07-05 11:23www.168986.cn编程入门
今天复习了JSP脚本元素和注释部分,案例写出来,大家自己调试下,整体如下,希望对大家有所帮助
今天复习了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代码发送到客户端显示的,狼蚁网站SEO优化给大家看下本案例的JSP页面转换成Servlet后的源码(其中红色标注为转换成前台的html代码)
/
Generated by the Jasper ponent 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 .apache.jsp;
import javax.servlet.;
import javax.servlet.http.;
import javax.servlet.jsp.;
public final class HelloXiaoMo_jsp extends .apache.jasper.runtime.HttpJspBase
implements .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 .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 = .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);
}
}
}
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代码发送到客户端显示的,狼蚁网站SEO优化给大家看下本案例的JSP页面转换成Servlet后的源码(其中红色标注为转换成前台的html代码)
代码如下:
/
Generated by the Jasper ponent 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 .apache.jsp;
import javax.servlet.;
import javax.servlet.http.;
import javax.servlet.jsp.;
public final class HelloXiaoMo_jsp extends .apache.jasper.runtime.HttpJspBase
implements .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 .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 = .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);
}
}
}
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程