我开始学习jsp,我发现,如果要在jsp中打印某些内容,则必须编写out.println()
而不是System.out.println()
,但是如果编写System.out.println()
,则不会显示任何错误,但也不会对浏览器进行操作。我想知道为什么会这样吗?众所周知,这System
是一个预定义的类,out
是连接到控制台的输出流。那么为什么我们不需要System
用jsp 编写呢?谢谢。
因为out
我们所指的不是System.out
,所以它是包装JSP页面的有效方法中的一个变量。System.out
写入servlet容器的控制台(通常是一个日志文件);out
是完全不同的类,它为生成的响应写入输出流。
当将JSP转换为代码时,它(从理论上来说,对于Tomcat而言)实际上要经历两个步骤:JSP-> servlet源代码,然后是servlet源代码->类。整个页面都放在一个方法中,在Tomcat中,它看起来像这样:
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);
}
}
如您所见,out
是该方法中的变量,类型为JspWriter
(而不是OutputStream
与System.out
)。
(附带说明:您包含在<%! ... %>
标记中而不是普通<% ... %>
标记中的代码未放入方法中;而是被放置在生成的Servlet类中的其他位置。)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句