JSP编译为Java类:
注意可以随便写import的内容;可以写类属性、方法、main函数、内部类;可以使用内部类;
JSP:
<%@ page language="java" import="java.util.*,a.b.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE html> <html> <% int cnt = 0; %> <head> <meta charset="UTF-8"> <title>004index</title> </head> <body> cnt is :<%=++cnt%> <hr> <%-- <%= String s = "foo"%>是不行的; --%> <%=new String[3]%> <hr> <%=getCnt()%> <hr> <%=new Foo()%> <%!int cnt = 0; public static void main(String args) { System.out.println("a"); } private int getCnt() {//如果返回void就无法使用表达式输出 return ++cnt; } class Foo { private String a; public Foo() { } public Foo(String a) { this.a = a; } public String getA() { return a; } public String toString() { return "Foo:" + a; } }%> </body> </html>
Java:
/* * Generated by the Jasper component of Apache Tomcat * Version: Apache Tomcat/7.0.35 * Generated at: 2015-10-12 00:25:50 UTC * Note: The last modified time of this file was set to * the last modified time of the source file after * generation to assist with modification tracking. */ package org.apache.jsp; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.jsp.*; import java.util.*; import a.b.*; public final class index004_jsp extends org.apache.jasper.runtime.HttpJspBase implements org.apache.jasper.runtime.JspSourceDependent { int cnt = 0; public static void main(String args) { System.out.println("a"); } private int getCnt() { return ++cnt; } class Foo{ private String a; public Foo(){} public Foo(String a){ this.a = a; } public String getA(){ return a; } public String toString(){ return "Foo:" + a; } } private static final javax.servlet.jsp.JspFactory _jspxFactory = javax.servlet.jsp.JspFactory.getDefaultFactory(); private static java.util.Map<java.lang.String,java.lang.Long> _jspx_dependants; private javax.el.ExpressionFactory _el_expressionfactory; private org.apache.tomcat.InstanceManager _jsp_instancemanager; public java.util.Map<java.lang.String,java.lang.Long> getDependants() { return _jspx_dependants; } public void _jspInit() { _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory(); _jsp_instancemanager = org.apache.jasper.runtime.InstanceManagerFactory.getInstanceManager(getServletConfig()); } public void _jspDestroy() { } public void _jspService(final javax.servlet.http.HttpServletRequest request, final javax.servlet.http.HttpServletResponse response) throws java.io.IOException, javax.servlet.ServletException { final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; // 注意这个out是JspWriter<java.io.Writer<java.lang.Object;不是Object>Writer>java.io.PrintWriter final java.lang.Object page = this; javax.servlet.jsp.JspWriter _jspx_out = null; javax.servlet.jsp.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(' '); out.write(' '); String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; out.write(" "); out.write("<!DOCTYPE html> "); out.write("<html> "); int cnt = 0; out.write(" "); out.write("<head> "); out.write("<meta charset="UTF-8"> "); out.write("<title>b index</title> "); out.write("</head> "); out.write("<body> "); out.write(" cnt is :"); out.print(++cnt); out.write(" "); out.write(" <hr> "); out.write(" "); out.write(' '); out.write(' '); out.write(' '); out.print(new String[3]); out.write(" "); out.write(" <hr> "); out.write(" "); out.print(getCnt()); out.write(" "); out.write(" <hr> "); out.write(" "); out.print(new Foo() ); out.write(' '); out.write(' '); out.write(' '); out.write(" "); out.write("</body> "); out.write("</html> "); } catch (java.lang.Throwable t) { if (!(t instanceof javax.servlet.jsp.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); else throw new ServletException(t); } } finally { _jspxFactory.releasePageContext(_jspx_page_context); } } }