如何解决从外部浏览器调用时的 HttpServletResponse 输出目标
我有一个在 Apache Tomcat (8.0.21.0) 下运行的 Java Web 应用程序。它的功能是监视各种外部进程并在浏览器中显示警报和定期更新状态。主要的 HTTP 请求处理程序很简单。
public class MyApplication extends HttpServlet
{
.
.
.
public void doPost (HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
processRequest (request,response);
}
public void doGet (HttpServletRequest request,response);
}
private static void processRequest (HttpServletRequest request,ServletException
{
String strOption = request.getParameter ("option");
int nOption = Integer.parseInt (strOption);
response.setContentType ("text/html");
PrintWriter out = response.getWriter ();
outputPage (out,nOption);
}
private void outputPage (PrintWriter out,int nOption)
{
out.println ("<!DOCTYPE html>");
out.println ("<html>");
out.println ("<head>");
switch (nOption)
{
// title,style and <body> content depend on option passed in request
.
.
.
}
out.println ("</body>);
out.println ("</html>");
}
}
但是,该应用程序还包括一个 TCP 侦听器和套接字,用于接收 IoT(物联网)消息:
public class MyTCPconnection extends Thread
{
public Socket clientSocket; // socket created by listener
private String url = "[local host address and servlet name]";
.
.
.
public void run ()
{
int RC = 400; // default value = "Bad Request"
try
{
// get bytes from remote process
int receiveBufferSize = clientSocket.getReceiveBufferSize ();
byte[] receiveBuffer = new byte[receiveBufferSize];
int bytesRead = TCPreceive (clientSocket,receiveBuffer); // not shown
if (bytesRead != -1 && bytesRead != 0)
{
String strOption = getOption (receiveBuffer); // not shown
}
HttpPost httpPost = new HttpPost (url);
httpPost.setHeader ("Accept","text/html,application/xhtml+xml,application/xml");
httpPost.setHeader ("Content-Type","application/x-www-form-urlencoded");
List<NameValuePair> requestParams = new ArrayList<NameValuePair>();
reqestParams.add (new BasicNameValuePair ("option",value));
CloseableHttpClient httpClient = HttpClients.createDefault ();
CloseableHttpResponse response = httpClient.execute (httpPost);
RC = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString (response.getEntity ());
system.out.println (responseBody);
}
catch (UnknownHostException e)
{
RC = 404;
}
catch (IOException e)
{
RC = 400;
}
TCPsend (clientSocket,RC); // reply to remote process,not shown
}
}
当然,MyTCPconnection.run() 会生成一个有效的 HTTP 请求体并向主应用程序提交一个 POST 请求。我遇到的问题是,如果 POST 是从 Web 浏览器(IExplorer、Firefox 等)进行的,应用程序会在浏览器中输出一个网页,但是在收到来自内部 MyTCPconnection 实例的 POST 请求时,它不会向任何浏览器。相反,它将整个输出重定向到 responseBody。
一开始我以为我只需要保存来自浏览器请求的 HttpServletResponse 和 PrintWriter 变量,并将保存的 PrintWriter 实例传递给函数 outputPage。但是,当我记录这些时,结果是:
来自浏览器的请求: HttpServletResponse 响应 = org.apache.catalina.connector.ResponseFacade@3e1d266b PrintWriter out = org.apache.catalina.connector.CoyoteWriter@6bc55aa8
来自 MyTCPconnection.run() 的请求: HttpServletResponse 响应 = org.apache.catalina.connector.ResponseFacade@3e1d266b PrintWriter out = org.apache.catalina.connector.CoyoteWriter@6bc55aa8
任何提示或帮助将不胜感激
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。