簡體   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