Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jsp

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.

like image 745
Deb Avatar asked May 01 '12 10:05

Deb


People also ask

Can we use System out Println in JSP?

out. println() Since the System object is part of the core Java objects, it can be used everywhere without the need to install any extra classes. This includes Servlets, JSP, RMI, EJB's, ordinary Beans and classes, and standalone applications.

What is the difference between out Println and System out Println?

println() - It is used to print the statement. The system is a class, out is the object of PrintStream class, println() is the method of PrintStream class which displays the result and then throws the cursor to the next line.

What is out Println in JSP?

println for printing anything on console. In JSP, we use only out. print to write something on console because the out we're referring to isn't System. out, it's a variable in the effective method that wraps our JSP page.

Why we should not use System out Println?

System. out. println is an IO-operation and therefor is time consuming. The Problem with using it in your code is, that your program will wait until the println has finished.


1 Answers

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

like image 64
T.J. Crowder Avatar answered Oct 13 '22 04:10

T.J. Crowder