我开始学习jsp,我看到了,如果我们想在jsp中打印一些内容,我们必须编写out.println()代替System.out.println(),但是如果我们编写System.out.println()它不会显示任何错误,但也不会对浏览器进行操作.我想知道它为什么会发生?正如我们所知,这System是一个预定义的类,out是连接到控制台的输出流.那么为什么我们不要求System用jsp 写?谢谢.
T.J*_*der 13
因为out我们所指的不是System.out,它是包装我们的JSP页面的有效方法中的变量.System.out写入servlet容器的控制台(通常是日志文件); out是一个完全不同的类,它为生成的响应写入输出流.
当JSP转变为代码时,它(理论上和Tomcat实际上)经历了两个步骤:JSP - > servlet源代码,然后是servlet源代码 - > class.整个页面放在一个方法中,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);
}
}
Run Code Online (Sandbox Code Playgroud)
正如可以看到,out是该方法中的变量,类型的JspWriter(而不是OutputStream如System.out).
(旁注:您在<%! ... %>代码中包含的代码而不是普通<% ... %>代码不会放在方法中;它会放在生成的servlet类的其他位置.)
| 归档时间: |
|
| 查看次数: |
49892 次 |
| 最近记录: |