JSP两种声明变量的区别

来源:互联网 发布:网络流行歌曲2009 编辑:程序博客网 时间:2024/05/17 04:47
在JSP中用两种声明变量的方法,一种是在<%! %>内,一种是在<% %>内。他们之间有什么区别呢?我们直接看一个JSP文件来理解。
count.jsp

代码如下:

<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><HTML><HEAD><TITLE>JSP Declarations</TITLE><BODY><H1>JSP Declarations</H1><%!  int count = 0;       //声明在<%!>内的变量       void method(){}     %><%  int i= 0;             //声明在<% >内的变量%><H1>count:<%= ++count %></H1><br/><H1>i:<%= ++i %></H1>     </BODY></HTML>

启动tomcat运行这个jsp会发现当我们不断刷新页面时count的值会不断增加 而i的值始终不变,也就是说定义在<%!%>内的变量会累加,而定义在<%%>内的变量不会累加。
这是为什么呢?
我们再看下count.jsp文件自动转换成的类文件的代码
文件所在地
TOMCAT安装目录/work/Catalina/localhost/你的工程名称/org/apache/jsp下的count_jsp.java

代码如下:

package org.apache.jsp;import javax.servlet.*;import javax.servlet.http.*;import javax.servlet.jsp.*;public final class count_jsp extends org.apache.jasper.runtime.HttpJspBase    implements org.apache.jasper.runtime.JspSourceDependent {  int count = 0;       //声明 在%!内的 变量        void method(){}     //方法  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=gb2312");      pageContext = _jspxFactory.getPageContext(this, request, response,         "", 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 XHTML 1.0 Transitional//EN/" /"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd/">/r/n");      out.write("<html xmlns=/"http://www.w3.org/1999/xhtml/">/r/n");      out.write("<head>/r/n");      out.write("<meta http-equiv=/"Content-Type/" content=/"text/html; charset=gb2312/" />/r/n");      out.write("<HTML>/r/n");      out.write("<HEAD>/r/n");      out.write("<TITLE>JSP Declarations</TITLE>/r/n");      out.write("<BODY>/r/n");      out.write("<H1>JSP Declarations</H1>/r/n");      out.write("/r/n");      out.write("/r/n");      out.write("/r/n");   int i= 0;             //声明在%内的变量       out.write("/r/n");      out.write("/r/n");      out.write("<H1>/r/n");      out.write("/r/n");      out.write("count:");      out.print( ++count );      out.write("</H1>/r/n");      out.write("<br/>/r/n");      out.write("/r/n");      out.write("<H1>i:");      out.print( ++i );      out.write("</H1>     /r/n");      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类中的局部变量用法一致。
原创粉丝点击