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客户程序)与服务器 ...
随机推荐
- 【HDOJ】2371 Decode the Strings
快速矩阵乘法.注意,原始字符串即为decode后的字符串.题目是要找到原始串. #include <cstdio> #include <cstring> #define MAX ...
- ♫【JS模式】偏函数
<深入浅出Node.js> var toString = Object.prototype.toString var isType = function(type) { return fu ...
- 【贪心】CSU 1809 Parenthesis (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 题目大意: 给一个长度为N(N<=105)的合法括号序列.Q(Q<= ...
- poj3469 最小割构图
题目链接:http://poj.org/problem?id=3469 #include <cstdio> #include <cmath> #include <algo ...
- + (void)load和+ (void)initialize有什么用处
两个方法都可以进行一些类的初始化操作.其中有些小区别.+(void)load 方法只要加入了工程种,进行了编译,且.m中实现了这个方法,都会调用一次,值得注意的时没实现的子类是不会调用的,就算父类实现 ...
- python环境配置selenium与IE、Chrome、Firefox、PhantomJS
安装.升级selenium pip install -U selenium 下载对应平台最新版的browser driver chrome: http://chromedriver.storage.g ...
- angular ng-class使用笔记
在前面Angularjs开发一些经验总结中说到在angular开发中angular controller never 包含DOM元素(html/css),在controller需要一个简单的POJO( ...
- url传递中文的解决方案
本文转载:http://www.cnblogs.com/ghd258/archive/2005/10/23/260241.html url传递中文的解决方案 1.设置web.config文件. < ...
- 解决"the currently displayed page contains invalid values"
原因是你的工程的根目录少了default.properties(有点项目工程这个文件名称是project.properties)这个文件,导致不能选择target: 解决办法: 在工程根目录下建立 ...
- Android图片框架---Glide
Glide *** 使用* 一.添加依赖* compile 'com.github.bumptech.glide:glide:3.7.0'* compile 'com.android.support: ...