Java 从外部浏览器调用时的HttpServletResponse输出终止

Java 从外部浏览器调用时的HttpServletResponse输出终止,java,servlets,tcp,tomcat8,Java,Servlets,Tcp,Tomcat8,我有一个在ApacheTomcat(8.0.21.0)下运行的Java web应用程序。它的功能是监视各种外部进程,并在浏览器中显示警报和定期更新的状态。主HTTP请求处理程序非常简单 public class MyApplication extends HttpServlet { . . . public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException,

我有一个在ApacheTomcat(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, HttpServletResponse response) throws IOException, ServletException
    {
        processRequest (request, response);
    }

    private static void processRequest (HttpServletRequest request, HttpServletResponse response) throws IOException, 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>");
    }
}
公共类MyApplication扩展了HttpServlet { . . . public void doPost(HttpServletRequest请求、HttpServletResponse响应)引发IOException、ServletException { processRequest(请求、响应); } public void doGet(HttpServletRequest请求,HttpServletResponse响应)抛出IOException,ServletException { processRequest(请求、响应); } 私有静态void processRequest(HttpServletRequest请求,HttpServletResponse响应)引发IOException,ServletException { String strOption=request.getParameter(“选项”); int nOption=Integer.parseInt(strOption); response.setContentType(“text/html”); PrintWriter out=response.getWriter(); outputPage(输出,无选项); } 私有void outputPage(PrintWriter输出,int-nOption) { out.println(“”); out.println(“”); out.println(“”); 开关(nOption) { //标题、样式和内容取决于请求中传递的选项 . . . } out.println(“); out.println(“”); } } 但是,该应用程序还包括TCP侦听器和套接字,用于接收物联网消息:

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扩展线程
{
公共套接字clientSocket;//侦听器创建的套接字
私有字符串url=“[本地主机地址和servlet名称]”;
.
.
.
公开作废运行()
{
int RC=400;//默认值=“错误请求”
尝试
{
//从远程进程获取字节
int receiveBufferSize=clientSocket.getReceiveBufferSize();
字节[]接收缓冲区=新字节[receiveBufferSize];
int bytesRead=tcprective(clientSocket,receiveBuffer);//未显示
if(bytesRead!=-1&&bytesRead!=0)
{
String strOption=getOption(receiveBuffer);//未显示
}
HttpPost HttpPost=新的HttpPost(url);
httpPost.setHeader(“接受”,“text/html,application/xhtml+xml,application/xml”);
httpPost.setHeader(“内容类型”、“应用程序/x-www-form-urlencoded”);
List requestParams=new ArrayList();
RequestParams.add(新的BasicNameValuePair(“选项”,值));
CloseableHttpClient-httpClient=HttpClients.createDefault();
CloseableHttpResponse response=httpClient.execute(httpPost);
RC=response.getStatusLine().getStatusCode();
字符串responseBody=EntityUtils.toString(response.getEntity());
system.out.println(响应库);
}
捕获(未知后异常e)
{
RC=404;
}
捕获(IOE异常)
{
RC=400;
}
TCPsend(clientSocket,RC);//回复远程进程,未显示
}
}
想当然地,MyTCPconnection.run()会生成一个有效的HTTP请求主体,并向主应用程序提交POST请求。我遇到的问题是,POST是从web浏览器(IExplorer、Firefox等)发出的,应用程序在浏览器中输出一个网页,但在从内部MyTCPconnection实例接收POST请求时,它不会向任何浏览器输出任何内容。相反,它会将整个输出重定向到响应库

起初我认为我只需要保存来自浏览器请求的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


如果有任何提示或hlp,我们将不胜感激。

为什么您认为编程HTTP应该在web浏览器中显示一些结果?在这种情况下,您的另一个应用程序部分是HTTP客户端,发送请求并获得响应。不涉及浏览器。如果我理解正确:当您的servlet
MyApplication
收到请求时,您希望要在内部将其转发到另一个servlet?为此,您已经有了一个servlet,它可以正确地转发请求,而不需要外部HTTP请求。