为什么我们在jsp中写out.println()而不是System.out.println()? [英] Why do we write out.println() in jsp instead of System.out.println()?

查看:42
本文介绍了为什么我们在jsp中写out.println()而不是System.out.println()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始学习jsp,我看到如果我们想用jsp打印一些东西,我们必须写out.println()而不是System.out.println(),但是如果我们编写 System.out.println() 它不会显示任何错误,但也不会向浏览器发送 o/p.我想知道为什么会这样?众所周知,System 是一个预定义的类,out 是连接到控制台的输出流.那么为什么我们不需要在jsp中编写System呢?谢谢.

I started learning jsp and I am seeing that, if we want to print something in jsp,we have to write out.println() instead of System.out.println(), but if we write System.out.println() it does not show any error but does not o/p to the browser also. I want to know why it happens? As all we know that System is a predefined class and out is the output stream connected to the console. So why we do not require to write System in jsp? Thanks.

推荐答案

因为我们所指的 out 不是 System.out,它是一个变量 in包装我们的 JSP 页面的有效方法.System.out 写入 servlet 容器的控制台(通常是日志文件);out 是一个完全不同的类,它为生成的响应写入输出流.

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.

当一个JSP变成代码时,它(理论上,实际上是Tomcat)要经过两个步骤:JSP -> servlet 源代码,然后是servlet 源代码-> 类.整个页面都放在一个方法中,在 Tomcat 中它看起来像这样:

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);
    }
}

如您所见,out 是该方法中的一个变量,属于 JspWriter 类型(而不是 OutputStreamSystem.out).

As you can see, out is a variable within that method, of type JspWriter (rather than OutputStream as with System.out).

(旁注:您包含在 <%! ... %> 标签中的代码,而不是普通的 <% ... %> 标签没有放在方法中;它放在生成的 servlet 类中的其他地方.)

(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.)

这篇关于为什么我们在jsp中写out.println()而不是System.out.println()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆