Why do we write out.println() in jsp instead of Sy

2019-01-14 04:09发布

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.

标签: java jsp
2条回答
Evening l夕情丶
2楼-- · 2019-01-14 04:29

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

查看更多
我只想做你的唯一
3楼-- · 2019-01-14 04:37

The out in jsp is a JspWriter object which is created by Jsp automatically, it is used to write something to webpage while System.out.print() is used to output/write something to the console.

查看更多
登录 后发表回答