繁体   English   中英

Java Servlet:PrintWriter带有变量的奇怪行为

[英]Java Servlet: PrintWriter strange behaviour with variable

我通过遵循这些1 2官方教程来学习如何编程servlet。 我找不到有关带变量的PrintWriter类的奇怪行为的简单问题的解决方案。 代码如下:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletExample extends HttpServlet {

   public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws IOException, ServletException
   {
    response.setContentType("text/html");       
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");  
    out.println("<title>First example</title>");
    out.println("</head>");
    out.println("<h2>System information</h2>");
    out.println("Host name: " + request.getRemoteHost() + "<br>");
    out.println("Remote address: " + request.getRemoteAddr() + "<br>");
    out.println("Port: " + request.getServerPort() + "<br>");
    out.println("Encoding: " + request.getCharacterEncoding() + "<br>");
    out.println("Method: " + request.getMethod() + "<br>");
    out.println("Protocol: " + request.getProtocol() + "<br>");
    out.println("Address: " + request.getRequestURI() + "<br>");
    out.println("Path: " + request.getPathInfo() + "<br>");
    out.println("</body>");
    out.println("</html>");
    out.close();
    }
}

servlet容器上servlet的输出显示到

out.println("<h2>System information</h2>");

我不知道为什么,但是我发现将以下行拆分为这些行是可行的:

out.println("Host name: ");
out.println(request.getRemoteHost())
out.println("<br>");

我在网上搜索了很多内容,但找不到并回答。

我在ubuntu mate 16.04 LTS下,从存储库安装了apache tomcat 8。

编辑我通过在ubuntu mate 16.04 LTS下尝试使用不同的tomcat 7以及使用xampp和tomcat 7的不同OS Windows 10来进一步研究此类行为。结果是相同的。

因此,我将出现的“请求信息”官方示例放到与tomcat一起安装的examples文件夹中。 该示例的代码是这样的:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RequestInfo extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Request Information Example</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h3>Request Information Example</h3>");
    out.println("Method: " + request.getMethod());
    out.println("Request URI: " + request.getRequestURI());
    out.println("Protocol: " + request.getProtocol());
    out.println("PathInfo: " + request.getPathInfo());
    out.println("Remote Address: " + request.getRemoteAddr());
    out.println("</body>");
    out.println("</html>");
}

/**
 * We are going to perform the same operations for POST requests
 * as for GET methods, so this method just sends the request to
 * the doGet method.
 */

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
    doGet(request, response);
}
}

如预期的那样,它不起作用,并且其行为与先前的代码相同。 但是,如果我在example文件夹中打开预构建版本,它将起作用。

最后,通过将每行分成几行,其工作方式如前所述。 这确实是奇怪的行为。

我建议您使用StringBuilder对象生成结果。 如果需要,您可以计算内容长度,这在某些情况下很有用。

StringBuilder sbText = new StringBuilder();
sbText.append("<html>")
    .append("<head>")
    .append(etc...);

String text = sbText.toString();
// Option A:
response.getWriter().print(text);

// Option B:
byte[] bytes = text.getBytes();
response.setContentLength(bytes.length);
response.getOutputStream().write(bytes);

我找到了解决我的奇怪问题的方法。 我在使用javac 1.9编译代码时也尝试了使用jvm v1.8.0_171-b11的最新tomcat v9.0.8。 通过使用javac v1.8,它可以工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM