jsp <%! %> 与 <% %> 区别
转自huangqiqing123.iteye.com/blog/1922014
<body>
<%! //1、可定义方法
public String outMethod(){
return "outMethod";
}
//2、可定义static方法
public static String outStaticMethod(){
return "outStaticMethod";
}
//3、可定义static属性
public static int count = 0; //4、不可以使用out对象
%> <%
//1、不可定义方法
//2、不可定义static方法
//3、不可定义static属性 //可以使用out对象
out.print(outMethod());
out.print("<br>");
out.print(outStaticMethod());
out.print("<br>");
out.print(count);
%>
</body>
<body>
<%! //1、可定义方法
public String outMethod(){
return "outMethod";
}
//2、可定义static方法
public static String outStaticMethod(){
return "outStaticMethod";
}
//3、可定义static属性
public static int count = 0; //4、不可以使用out对象
%> <%
//1、不可定义方法
//2、不可定义static方法
//3、不可定义static属性 //可以使用out对象
out.print(outMethod());
out.print("<br>");
out.print(outStaticMethod());
out.print("<br>");
out.print(count);
%>
</body>
经Tomcat编译后,生成的java文件如下:
package org.apache.jsp; import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*; public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent { //1、可定义方法
public String outMethod(){
return "outMethod";
}
//2、可定义static方法
public static String outStaticMethod(){
return "outStaticMethod";
}
//3、可定义static属性
public static int count = 0; //4、不可以使用out对象 private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory(); private static java.util.List _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory;
private org.apache.AnnotationProcessor _jsp_annotationprocessor; public Object getDependants() {
return _jspx_dependants;
} public void _jspInit() {
_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
} public void _jspDestroy() {
} public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException { PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
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("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r\n");
out.write("<html>\r\n");
out.write("<head>\r\n");
out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\r\n");
out.write("<title>Insert title here</title>\r\n");
out.write("</head>\r\n");
out.write("<body>\r\n");
out.write("\r\n");
out.write("\r\n"); //1、不可定义方法
//2、不可定义static方法
//3、不可定义static属性 //可以使用out对象
out.print(outMethod());
out.print("<br>");
out.print(outStaticMethod());
out.print("<br>");
out.print(count); out.write("\r\n");
out.write("</body>\r\n");
out.write("</html>");
} catch (Throwable t) {
if (!(t instanceof 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);
}
} finally {
_jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
在生成的java文件中,我们可以看到,<%! %>中的代码对应于java类文件的成员方法、静态方法,静态变量,而<% %>中的代码对应于java类文件的_jspService方法中的代码(_jspService对应于servlet中的service方法), 这也就是为什么在<% %>中不能声明方法、静态变量的原因了(方法中是不可以再创建方法的)。
jsp <%! %> 与 <% %> 区别的更多相关文章
- JS和JSP的区别
最近很多同学在纠结于名词缩写之间的相似性,因此本人也来写一篇,讲讲JS和JSP的区别. SUN首先发展出SERVLET,其功能比较强劲,体系设计也很先进,只是,它输出HTML语句还是采用了老的CGI方 ...
- Servlet与JSP的区别(转)
原文链接:Servlet与JSP的区别 两者之间的联系和区别 [1]JSP第一次运行的时候会编译成Servlet,驻留在内存中以供调用. [2]JSP是web开发技术,Servlet是服务器端运用的小 ...
- 6.servlet和jsp的区别
servlet和jsp的区别 jsp作为Servlet技术的扩展,经常会有人将jsp和Servlet搞混.本文,将为大家带来servlet和jsp的区别,希望对大家有所帮助. servlet和jsp的 ...
- JSP serverlet区别与联系
jsp是html包含java servlet是java包含html jsp请求到tomcat---tomcat封装了jsp到servlet实现. 所以jsp请求时候,会自动创建session 而不用在 ...
- html 与 jsp 文件格式区别
html 与 jsp 文件格式区别 html5 <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...
- Servlet概念及与Jsp的区别
一.Servlet概念 Servlet是在服务器上运行的小程序.一个Servlet就是一个Java类,并且可以通过”请求-响应”编程模型来访问这个驻留在服务器内存里的Servlet程序 二.Servl ...
- jsp中的@include与jsp:include区别详解
1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...
- Servlet与JSP的区别
一.基本概念 1.1 Servlet Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面.它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器 ...
- Servlet的生命周期,并说出Servlet和CGI的区别,Servlet与JSP的区别
一.Servlet 生命周期 1.加载 2.实例化 3.初始化 4.处理请求 5.销毁 二.Servlet与cgi的区别: Servlet处于服务器进程中,它通过多线程方式运行其service方法,一 ...
- Java从入门到精通——基础篇之Servlet与JSP的区别
一.基本概念 1.1 Servlet Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面.它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器 ...
随机推荐
- C++ STL的各种实现版本
ANSI/ISO的C++ STL规范版本正式通过以后,各个C++编译器厂商就可以依照标准所描述的原型去实现C++ STL泛型库,于是出现多种符合标准接口,但具体实现代码不同的泛型库,主要有: HP S ...
- Light OJ 1018 - Brush (IV)
题目大意: 一个二维平面上有N个点,一把刷子,刷一次可以把一条线上的所有点都刷掉.问最少刷多少次,可以把全部的点都刷完 状态压缩DP, 用记忆化搜索来写, 需要有个优化不然会超时. ===== ...
- Light OJ 1005 - Rooks(DP)
题目大意: 给你一个N和K要求确定有多少种放法,使得没有两个车在一条线上. N*N的矩阵, 有K个棋子. 题目分析: 我是用DP来写的,关于子结构的考虑是这样的. 假设第n*n的矩阵放k个棋子那么,这 ...
- 往github上传demo
一直在github上寻找demo,但怎么传demo上githun呢? http://www.2cto.com/kf/201504/390397.html 首先在github上 new一个reposit ...
- 【动态规划】【KMP】HDU 5763 Another Meaning
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5763 题目大意: T组数据,给两个字符串s1,s2(len<=100000),s2可以被解读成 ...
- 数据结构——foodfill 八连块问题
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- 吐槽iOS国际化:关于NSLocalizedString的使用
http://blog.csdn.net/aries4ever/article/details/8185405
- today's learning of english 1
1.curriculum 必修课 主修课 2.sought seek的过去式 3.blessed with a wonderful marriage 有个幸福美满的婚姻 4.al ...
- 使用IndexReader.repen提高搜索速度
1,使用indexreader创建indexsearcher. 2,indexsearcher在使用完了以后不要关闭. 3.使用indexreader.isCurrent()判断索引是否被indexw ...
- 在SQL中使用PL/SQL函数存在的问题
-----------------------------Cryking原创------------------------------ -----------------------转载请注明出处, ...