简体   繁体   English

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

[英]Java Servlet: PrintWriter strange behaviour with variable

I'm learning how to program servlet by following these 1 2 official tutorials. 我通过遵循这些1 2官方教程来学习如何编程servlet。 I cannot find a solution to a simple question about the strange behaviour of PrintWriter class with variables. 我找不到有关带变量的PrintWriter类的奇怪行为的简单问题的解决方案。 The code is the following: 代码如下:

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();
    }
}

The output of the servlet on servlet container is displayed up to servlet容器上servlet的输出显示到

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

I cannot figure out why, but I discovered that by splitting the following line into these lines works: 我不知道为什么,但是我发现将以下行拆分为这些行是可行的:

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

I searched a lot on the Web, but I cannot find and answer. 我在网上搜索了很多内容,但找不到并回答。

I'm under ubuntu mate 16.04 LTS with apache tomcat 8 installed from repository. 我在ubuntu mate 16.04 LTS下,从存储库安装了apache tomcat 8。

Edit I further investigate about such behaviour by trying different tomcat 7 under ubuntu mate 16.04 LTS and different OS windows 10 with xampp and tomcat 7. The result is the same. 编辑我通过在ubuntu mate 16.04 LTS下尝试使用不同的tomcat 7以及使用xampp和tomcat 7的不同OS Windows 10来进一步研究此类行为。结果是相同的。

So I took the Request Info official example present into examples folder installed together with tomcat. 因此,我将出现的“请求信息”官方示例放到与tomcat一起安装的examples文件夹中。 The code of the example is this: 该示例的代码是这样的:

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);
}
}

As expected it does not work and its behaviour it is the same of the previous code. 如预期的那样,它不起作用,并且其行为与先前的代码相同。 However, if I open prebuilt version inside the example folder it works. 但是,如果我在example文件夹中打开预构建版本,它将起作用。

Finally, by splitting each line into several lines it works as described before. 最后,通过将每行分成几行,其工作方式如前所述。 It is really strange behaviour. 这确实是奇怪的行为。

I would recommend that you use a StringBuilder object to generate the result, then. 我建议您使用StringBuilder对象生成结果。 You can, if desired, calculate the content length, which is useful in some situations. 如果需要,您可以计算内容长度,这在某些情况下很有用。

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

I found the solution at my strange problem. 我找到了解决我的奇怪问题的方法。 I tried also with latest tomcat v9.0.8 that uses jvm v1.8.0_171-b11 while I compiled my code with javac 1.9. 我在使用javac 1.9编译代码时也尝试了使用jvm v1.8.0_171-b11的最新tomcat v9.0.8。 By using javac v1.8 it works. 通过使用javac v1.8,它可以工作。

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

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