Because the out
we're referring to isn't System.out
, it's a variable in the effective method that wraps our JSP page. System.out
writes to the servlet container's console (usually a log file); out
is a different class entirely which writes to the output stream for the generated response.
When a JSP is turned into code, it (in theory, and with Tomcat, in fact) goes through two steps: JSP -> servlet source code, then servlet source code -> class. The entire page is put inside a method, which with Tomcat looks something like this:
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");
pageContext = _jspxFactory.getPageContext(this, request, response,
"qdforumerror.jsp", true, 65536, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
/* =============================================
...your <% ... %> JSP code here, with
any markup outside those tags converted into
out.print("..."); statments...
=============================================
*/
}
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);
}
}
As you can see, out
is a variable within that method, of type JspWriter
(rather than OutputStream
as with System.out
).
(Side note: Code you include in <%! ... %>
tags rather than the normal <% ... %>
tags isn't put in the method; it's put elsewhere in the generated servlet class.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…