转自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. Linux下的定时器:alarm()与setitimer()

    Linux下的定时器有两种,以下分别介绍: 1.alarm 如果不要求很精确的话,用alarm()和signal()就够了 unsigned int alarm(unsigned int second ...

  2. 数据结构(KD树):HDU 4347 The Closest M Points

    The Closest M Points Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Ot ...

  3. 图论(网络流):COGS 410. [NOI2009] 植物大战僵尸

    410. [NOI2009] 植物大战僵尸 ★★★   输入文件:pvz.in   输出文件:pvz.out   简单对比时间限制:2 s   内存限制:512 MB [问题描述] Plants vs ...

  4. HDOJ(HDU) 2153 仙人球的残影(谜一样的题、、、)

    Problem Description 在美丽的HDU,有一名大三的同学,他的速度是众所周知的,跑100米仅仅用了2秒47,在他跑步过程中会留下残影的哎,大家很想知道他是谁了吧,他叫仙人球,既然名字这 ...

  5. pgAdminIII使用图解

    原文地址:http://www.2cto.com/database/201312/267218.html pgAdmin III简介 要打开一个到服务的连接,在树中选择所需的服务,并双击它,或使用“工 ...

  6. C++引用变量学习

    版权所有,转载请注明来源 (1)reference variable(rv) 主要用处是作为方程的形式参数,使用rv 可以直接对原数据进行操作而不是该数据的拷贝,节省了时间和空间,尤其是对于结构体以及 ...

  7. 《C专家编程》读书笔记

    第一章 const float* 表示一个指向float类型常量的指针 第二章 1. 在c语言中const并非真正表示“常量”,在数组定义与case中不可以使用 2. case的一些问题 1: #in ...

  8. 从git上下载代码并导入eclipse

    主要分为两步: 1.先从git下载代码到本地git仓库 2.eclipse import导入存在的maven项目

  9. Linux下profile environment bashrc的区别

        先将export LANG=zh_CN加入/etc/profile ,退出系统重新登录,登录提示显示英文.将/etc/profile 中的export LANG=zh_CN删除,将LNAG=z ...

  10. 69个spring面试题及答案

    Spring 概述 1. 什么是spring? Spring 是个Java企业级应用的开源开发框架.Spring主要用来开发Java应用,但是有些扩展是针对构建J2EE平台的web应用.Spring ...