Jetty - 寻求EOF时端点输出未关闭

时间:2012-12-15 13:26:16

标签: java java-ee jetty

访问此Servlet时,我总是在Jetty 9中收到此警告(使用GET或POST):

package de.swt1321.servlet;

import java.util.ArrayList;

import java.io.OutputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletTest extends HttpServlet {
    private static final int UNPROCESSABLE_ENTITY = 422;
    private static final java.nio.charset.Charset UTF8 = java.nio.charset.Charset.forName("UTF8");
    private ArrayList<Person> persons = new ArrayList<Person>(); 
    @Override
    public void doGet(HttpServletRequest req,
                          HttpServletResponse res) throws ServletException, IOException
    {
        byte [] HTML = genHTML();
        res.setStatus(HttpServletResponse.SC_OK);
        res.setHeader("content-type","text/html;charset=utf8");
        res.setIntHeader("content-length",HTML.length);
        OutputStream os = res.getOutputStream();
        os.write(HTML);
        os.flush();
    }

    @Override
    public void doPost(HttpServletRequest req,
                        HttpServletResponse res) throws ServletException, IOException
    {
        String firstName = req.getParameter("firstName");
        String lastName = req.getParameter("lastName");
        if(firstName == null || lastName == null) {
            res.setStatus(UNPROCESSABLE_ENTITY);
            return;
        }
        persons.add(new Person(req.getParameter("firstName"),req.getParameter("lastName")));
        byte[] HTML = genHTML();
        res.setStatus(HttpServletResponse.SC_OK);
        res.setHeader("content-type","text/html;charset=utf8");
        res.setIntHeader("content-length",HTML.length);
        OutputStream os = res.getOutputStream();
        os.write(HTML);
        os.flush();
    }

    private byte[] genHTML() {
        String HTMLString = "<!DOCTYPE html>"+
            "<html>"+
            "<head>" +
            "<title>Hello World!</title>"+
            "<style>" +
            "table,th,tr { border: 1px solid black;}"+
            "</style>" +
            "</head>"+
            "<body><table><tr><th>First Name</th><th>Last Name</th></tr>";
        for(Person person : persons) {
            HTMLString += "<tr><td>"+person.firstName+"</td><td>"+person.lastName+"</td></tr>";
        }
        HTMLString += "</table>"+
            "<form name='input' action='/ServletTest-1.0.0/hello' method='post'>"+
            "<input type='text' name='firstName'/>"+
            "<input type='text' name='lastName'/>" +
            "<input type='submit' value='Send me!'/>"+
            "</form>" +
            "</body>" +
            "</html>";
        return HTMLString.getBytes(UTF8);
    }

    private static class Person {
        public String firstName;
        public String lastName;
        public Person(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }
    }
}

的web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0"> 

    <servlet>
        <servlet-name>HelloWorldServlet</servlet-name>
        <display-name>Hello World App</display-name>
        <load-on-startup>1</load-on-startup>
        <servlet-class>de.swt1321.servlet.ServletTest</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloWorldServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

</web-app>

这是我应该担心的吗?如果是,我该如何解决这个问题?这甚至意味着什么?

2 个答案:

答案 0 :(得分:1)

这是因为您正在刷新输出流。 只需删除op.flush();让Jetty处理它

答案 1 :(得分:0)

OutputStream os = res.getOutputStream().write(HTML);

除非您打算自己处理输出流的生命周期,否则不要费心保留本地引用。