Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP/Java/HTML | JSP out.println(); prints to console when in method

I'm working a dynamic website with jsp.

Now my problem: when I use <%, to write my java, everything works perfectly fine.

<%
   out.println("<p>test</p>");
%>

But when i use the <%! like this:

<%!
  private void test() {
   out.println("<p>test</p>");
}
%>

My output will get displayed in my code editors console and not on my website as expected.

As import I used <%@ page import="static java.lang.System.out" %>. Is this the correct import or is the problem somewhere else?

If more information is needed please comment! :)

like image 366
WetWer Avatar asked Feb 08 '23 06:02

WetWer


1 Answers

As you probably know, JSPs are turned into servlets on-the-fly by the Java EE container. In a <% ... %> block, out is a local variable in the generated _jspService (or similar) method in the generated servlet. It's a JspWriter for writing to the output for the page.

In a <%! ... %> block, you're outside that generated _jspService (or similar) method, and so your static import means your out reference is to System.out, which isn't where the page output should be sent.

If you want to define methods in your JSP in <%! ... %> blocks, you'll have to pass out into them:

<%!
private void test(JspWriter out) throws IOException {
    out.println("<p>test</p>");
}
%>

About that JSP -> servlet thing, say we have this JSP:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
</head>
<body>
<%
    out.println("The current date/time is " + new java.util.Date());
    this.test(out, "Hi, Mom!");
%>
<%!
    private void test(JspWriter out, String msg) throws java.io.IOException {
        out.println(msg);
    }
%>
</body>
</html>

Note that it has a <%...%> block and a <%! ... %> block.

The Java EE container turns that into something somewhat like the following. Note where our test method ended up, and where the code in our <%...%> block ended up (along with our raw JSP text/markup):

package org.apache.jsp;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;

public final class test_jsp extends org.apache.jasper.runtime.HttpJspBase
    implements org.apache.jasper.runtime.JspSourceDependent {

    private void test(JspWriter out, String msg) throws java.io.IOException {
        out.println(msg);
    }

    /* ...lots of setup stuff omitted... */

    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,
                        null, true, 8192, true);
            _jspx_page_context = pageContext;
            application = pageContext.getServletContext();
            config = pageContext.getServletConfig();
            session = pageContext.getSession();
            out = pageContext.getOut();
            _jspx_out = out;

            out.write("<!doctype html>\n");
            out.write("<html>\n");
            out.write("<head>\n");
            out.write("<meta charset=\"utf-8\">\n");
            out.write("<title>Example</title>\n");
            out.write("</head>\n");
            out.write("<body>\n");

            out.println("The current date/time is " + new java.util.Date());
            this.test(out, "Hi, Mom!");

            out.write("\n");
            out.write("</body>\n");
            out.write("</html>\n");
        } 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);
                else log(t.getMessage(), t);
            }
        } finally {
            _jspxFactory.releasePageContext(_jspx_page_context);
        }
    }
}
like image 198
T.J. Crowder Avatar answered Feb 15 '23 22:02

T.J. Crowder