Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JSP/Java/HTML | JSP out.println();在方法中时打印到控制台_Java_Html_Jsp_Tomcat - Fatal编程技术网

JSP/Java/HTML | JSP out.println();在方法中时打印到控制台

JSP/Java/HTML | JSP out.println();在方法中时打印到控制台,java,html,jsp,tomcat,Java,Html,Jsp,Tomcat,我正在用jsp开发一个动态网站 现在我的问题是:当我使用 但是当我使用 我的输出将显示在我的代码编辑器控制台中,而不是像预期的那样显示在我的网站上 作为导入,我使用了。这是正确的输入还是其他地方的问题 如果需要更多信息,请发表评论!:) 您可能知道,Java EE容器会动态地将JSP转换为servlet。在块中,out是生成的servlet中生成的\u jspService(或类似)方法中的局部变量。它是一个JspWriter,用于写入页面的输出 在块中,您不在生成的\uJSPService

我正在用jsp开发一个动态网站

现在我的问题是:当我使用

但是当我使用

我的输出将显示在我的代码编辑器控制台中,而不是像预期的那样显示在我的网站上

作为导入,我使用了
。这是正确的输入还是其他地方的问题


如果需要更多信息,请发表评论!:)

您可能知道,Java EE容器会动态地将JSP转换为servlet。在
块中,
out
是生成的servlet中生成的
\u jspService
(或类似)方法中的局部变量。它是一个
JspWriter
,用于写入页面的输出

块中,您不在生成的
\uJSPService
(或类似)方法的范围内,因此静态导入意味着您的
out
引用是指向
System.out
,而不是页面输出应该发送的位置

如果您想在JSP的
块中定义方法,您必须将
传递给它们:

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

没有什么是正确的。你应该避免使用Scripplet。总是使用JSTLIt从“java.lang”导入任何东西都很奇怪,因为默认情况下已经导入了它;静态导入也被搁置(我只在为JUnit生成的代码中看到)。在任何地方使用
System.out.println
都更正常。无论如何,你应该认真考虑阅读关于JSP的手册/教程,因为看起来你真的不知道你在做什么。但我会灵巧地来到JSTL。
<%!
  private void test() {
   out.println("<p>test</p>");
}
%>
<%!
private void test(JspWriter out) throws IOException {
    out.println("<p>test</p>");
}
%>
<!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>
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);
        }
    }
}