转自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 <%! %> 与 <% %> 区别的更多相关文章

  1. JS和JSP的区别

    最近很多同学在纠结于名词缩写之间的相似性,因此本人也来写一篇,讲讲JS和JSP的区别. SUN首先发展出SERVLET,其功能比较强劲,体系设计也很先进,只是,它输出HTML语句还是采用了老的CGI方 ...

  2. Servlet与JSP的区别(转)

    原文链接:Servlet与JSP的区别 两者之间的联系和区别 [1]JSP第一次运行的时候会编译成Servlet,驻留在内存中以供调用. [2]JSP是web开发技术,Servlet是服务器端运用的小 ...

  3. 6.servlet和jsp的区别

    servlet和jsp的区别 jsp作为Servlet技术的扩展,经常会有人将jsp和Servlet搞混.本文,将为大家带来servlet和jsp的区别,希望对大家有所帮助. servlet和jsp的 ...

  4. JSP serverlet区别与联系

    jsp是html包含java servlet是java包含html jsp请求到tomcat---tomcat封装了jsp到servlet实现. 所以jsp请求时候,会自动创建session 而不用在 ...

  5. html 与 jsp 文件格式区别

    html 与 jsp 文件格式区别 html5 <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  6. Servlet概念及与Jsp的区别

    一.Servlet概念 Servlet是在服务器上运行的小程序.一个Servlet就是一个Java类,并且可以通过”请求-响应”编程模型来访问这个驻留在服务器内存里的Servlet程序 二.Servl ...

  7. jsp中的@include与jsp:include区别详解

    1 前言 搞java开发的人也许都知道在jsp中引入项目中其他文件有如下两种方式 <%@include file="xxx.jsp"%> <jsp:include ...

  8. Servlet与JSP的区别

    一.基本概念 1.1 Servlet Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面.它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器 ...

  9. Servlet的生命周期,并说出Servlet和CGI的区别,Servlet与JSP的区别

    一.Servlet 生命周期 1.加载 2.实例化 3.初始化 4.处理请求 5.销毁 二.Servlet与cgi的区别: Servlet处于服务器进程中,它通过多线程方式运行其service方法,一 ...

  10. Java从入门到精通——基础篇之Servlet与JSP的区别

    一.基本概念 1.1 Servlet Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面.它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器 ...

随机推荐

  1. AlgorithmsI Programming Assignment 1: PercolationStats.java

    import edu.princeton.cs.algs4.StdOut; import edu.princeton.cs.algs4.StdRandom; import edu.princeton. ...

  2. BZOJ 1055 [HAOI2008]玩具取名

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1119  Solved: 653[Submit][Statu ...

  3. Visual Studio Code尝试体验

    背景了解 偶然间看到一篇大赞Visual Studio Code的文章,就搜索了一下,发现网上基本一致的好评.虽然微软在2015年4月29号 Build 2015 大会上才发布,但免费,轻量,跨平台版 ...

  4. uboot从SD卡烧写内核和文件系统

    环境:ubuntu 13.04一.首先制作sd启动盘: 插入SD卡    sudo dd iflag=dsync oflag=dsync if=tiny210v2-uboot.binof=/dev/m ...

  5. Computer Vision Algorithm Implementations

    Participate in Reproducible Research General Image Processing OpenCV (C/C++ code, BSD lic) Image man ...

  6. windows下sqlite3静态库和动态库的编译

    1.下载sqlite3源码:http://www.sqlite.org/download.html 主要是sqlite-amalgamation-XXXXXXX.zip.sqlite-dll-win3 ...

  7. 《University Calculus》-chaper8-无穷序列和无穷级数-p级数

    Q:定义p级数有如下形式,讨论p级数的敛散性.(p>o) 我们以p = 1作为分界点,因为实践表明这个分界点是最优区分度的.那么下面我们进行分情况讨论. 在这之前,我们有必要先引入一个检验敛散性 ...

  8. javascript随机将第一个dom中的图片添加到第二个div中去

    javascript随机将第一个dom中的图片添加到第二个div中去,此代码的是一个简单的例子,将第一个div中的五张图片中,提取随机两张显示到第二个div中. <!DOCTYPE html P ...

  9. angularJS测试一 Karma Jasmine Mock

    AngularJS测试 一 测试工具 1.NodeJS领域:Jasmine做单元测试,Karma自动化完成单元测试,Grunt启动Karma统一项目管理,Yeoman最后封装成一个项目原型模板,npm ...

  10. Genymotion开始搞起~

    简介 一:什么是GenymotionGenymotion是一款完全超越BlueStacks的安卓模拟器,正如它中文官网的介绍:快到极致的Android模拟器.英文官网:http://www.genym ...