jsp中<%! %>
1 <body> 2 <%! 3 4 //1、可定义方法 5 public String outMethod(){ 6 return "outMethod"; 7 } 8 //2、可定义static方法 9 public static String outStaticMethod(){ 10 return "outStaticMethod"; 11 } 12 //3、可定义static属性 13 public static int count = 0; 14 15 //4、不可以使用out对象 16 %> 17 18 <% 19 //1、不可定义方法 20 //2、不可定义static方法 21 //3、不可定义static属性 22 23 //可以使用out对象 24 out.print(outMethod()); 25 out.print("<br>"); 26 out.print(outStaticMethod()); 27 out.print("<br>"); 28 out.print(count); 29 %> 30 </body>
经Tomcat编译后,生成的java文件如下:
1 package org.apache.jsp; 2 3 import javax.servlet.*; 4 import javax.servlet.http.*; 5 import javax.servlet.jsp.*; 6 7 public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase 8 implements org.apache.jasper.runtime.JspSourceDependent { 9 10 11 12 //1、可定义方法 13 public String outMethod(){ 14 return "outMethod"; 15 } 16 //2、可定义static方法 17 public static String outStaticMethod(){ 18 return "outStaticMethod"; 19 } 20 //3、可定义static属性 21 public static int count = 0; 22 23 //4、不可以使用out对象 24 25 private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory(); 26 27 private static java.util.List _jspx_dependants; 28 29 private javax.el.ExpressionFactory _el_expressionfactory; 30 private org.apache.AnnotationProcessor _jsp_annotationprocessor; 31 32 public Object getDependants() { 33 return _jspx_dependants; 34 } 35 36 public void _jspInit() { 37 _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); 38 _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName()); 39 } 40 41 public void _jspDestroy() { 42 } 43 44 public void _jspService(HttpServletRequest request, HttpServletResponse response) 45 throws java.io.IOException, ServletException { 46 47 PageContext pageContext = null; 48 HttpSession session = null; 49 ServletContext application = null; 50 ServletConfig config = null; 51 JspWriter out = null; 52 Object page = this; 53 JspWriter _jspx_out = null; 54 PageContext _jspx_page_context = null; 55 56 57 try { 58 response.setContentType("text/html; charset=utf-8"); 59 pageContext = _jspxFactory.getPageContext(this, request, response, 60 null, true, 8192, true); 61 _jspx_page_context = pageContext; 62 application = pageContext.getServletContext(); 63 config = pageContext.getServletConfig(); 64 session = pageContext.getSession(); 65 out = pageContext.getOut(); 66 _jspx_out = out; 67 68 out.write(" "); 69 out.write("<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> "); 70 out.write("<html> "); 71 out.write("<head> "); 72 out.write("<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> "); 73 out.write("<title>Insert title here</title> "); 74 out.write("</head> "); 75 out.write("<body> "); 76 out.write(" "); 77 out.write(" "); 78 79 //1、不可定义方法 80 //2、不可定义static方法 81 //3、不可定义static属性 82 83 //可以使用out对象 84 out.print(outMethod()); 85 out.print("<br>"); 86 out.print(outStaticMethod()); 87 out.print("<br>"); 88 out.print(count); 89 90 out.write(" "); 91 out.write("</body> "); 92 out.write("</html>"); 93 } catch (Throwable t) { 94 if (!(t instanceof SkipPageException)){ 95 out = _jspx_out; 96 if (out != null && out.getBufferSize() != 0) 97 try { out.clearBuffer(); } catch (java.io.IOException e) {} 98 if (_jspx_page_context != null) _jspx_page_context.handlePageException(t); 99 } 100 } finally { 101 _jspxFactory.releasePageContext(_jspx_page_context); 102 } 103 } 104 }
在生成的java文件中,我们可以看到,<%! %>中的代码对应于java类文件的成员方法、静态方法,静态变量,而<% %>中的代码对应于java类文件的_jspService方法中的代码(_jspService对应于servlet中的service方法),这也就是为什么在<% %>中不能声明方法、静态变量的原因了(方法中是不可以再创建方法的)