http Java servlet发送回响应
我正在尝试将一些文本从服务器端发送回客户端。我试过回应。请回答。但它们都不起作用。我需要帮助
这是我的客户:
public static void main(String[] args) throws MalformedURLException, IOException {
URL url = new URL("http://localhost:8080/WebServiceDesignStyles3ProjectServer/NewServlet/www");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Accept", "text/xml");
con.setDoOutput(true);
con.setDoInput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeUTF("aaa");
wr.flush();
wr.close();
InputStream is = con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while ((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
}
rd.close();
System.out.println(response);
System.out.println(con.getResponseCode());
System.out.println(con.getResponseMessage());
}
}
以下是我在服务器上的doGET方法:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//processRequest(request, response);
response.setStatus(404);
response.setContentType("text/xml");
PrintWriter writer=response.getWriter();
writer.append("this is 404");
writer.flush();
}
但我的客户仍然打印出200和OK,这是默认值。我怎样才能把信息发送回客户呢
编辑:
问题解决了,我不该要求多吉特的尸体
谢谢
# 1 楼答案
当您将
doOutput
设置为true
时,您还将request方法设置为POST:see the Javadoc。所以你的doGet()
方法没有被调用尝试将GET与请求的输出结合起来是没有意义的。GET请求不能有请求正文