JSP的变量
jsp 的变量有两声明方法,
1, 写在 <%! %>里
2, 写在 <% %>里
注意它们一个有"!" , 另一个没有。它们的区别是:用第一种方法声明的变量将被翻译成 servlet 的类变量,用第二方法声明的变量将翻译成方法变量。如下边所示:
jsp文件:
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<body>
<%! int i = 100 ; %>
<% int j = 1000 ; %>
<% out.print( " i=" + i + "; j=" + j ) ; %> </p>
</body>
</html>
生成的 servlet :
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 {
int i = 100 ;
private static java.util.List _jspx_dependants;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
JspFactory _jspxFactory = null;
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 {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html;charset=GB2312");
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("/r/n");
out.write("/r/n");
out.write("<html>/r/n");
out.write("<body>/r/n");
out.write("/t");
out.write('/r');
out.write('/n');
out.write(' ');
int j = 1000 ;
out.write('/r');
out.write('/n');
out.write(' ');
out.print( " i=" + i + "; j=" + j ) ;
out.write(" </p>/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)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}