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

查看:164
本文介绍了为什么我们在jsp而不是System.out.println()中写出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 ,它是包装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源代码 - > class。整个页面放在一个方法中,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 (而不是 OutputStream ,如同 System.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而不是System.out.println()中写出out.println()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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