gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 11:37:50 24 4
gpt4 key购买 nike

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

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>

这是我应该担心的事情吗?如果是,我该如何解决?这究竟意味着什么?

最佳答案

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

关于java - Jetty - 寻找 EOF 时端点输出未关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13892606/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com