2016-08-02 24 views
0

我的目标是从Java应用程序(JA)向servlet(S)发送简单消息并将其显示在浏览器页面上。我希望每次运行servlet类(刷新浏览器页面)时,都会收到消息。将消息从Java应用程序发送到servlet并显示它

我使用BufferedWriter在S上传输JA和BufferedReader上的输出以接收它,但我想我做错了什么。

下面是从JA的代码片段:

try{ 
     URL url = new URL("http://(/*ip address*/:8080/FirstServlet/myfirstservlet");     
     URLConnection conn = url.openConnection();     
     conn.setDoOutput(true); 
     conn.setDoInput(true);     
     BufferedWriter out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); 
     out.write("testing");    
     out.flush(); 
     out.close();   
     BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));    
     String response; 
     while((response = in.readLine()) != null) { 
      //System.out.println(response); 
     } 
     in.close(); 
     } 
     catch (MalformedURLException ex) { 
      ex.printStackTrace(); 
     } 
     catch (IOException iex) { 
      iex.printStackTrace(); 
     }   

下面是从S码:

import javax.servlet.*; 
import javax.servlet.http.*; 
import java.io.*; 
import java.util.*; 

public class FirstServlet extends HttpServlet { 
    private final static String _USERNAME = "username"; 

    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException , IOException { 

     PrintWriter out = res.getWriter(); 


     BufferedReader bufferedReader = null; 
     StringBuilder stringBuilder = new StringBuilder(); 
     char test = 'a'; 
     try{ 
      InputStream inputStream = req.getInputStream(); 

      if (inputStream != null) { 
       bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 

       char[] charBuffer = new char[128]; 
       int bytesRead = -1; 

       while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { 
        stringBuilder.append(charBuffer, 0, bytesRead); 
        out.println(charBuffer); //expecting this to be displayed on browser 
       } 
      } else { 
       stringBuilder.append(""); 
       out.println("0"); 
      } 
     } catch (IOException ex) { 
      ex.printStackTrace(); 
     } 

    } 

我试图在同时包装我的JA的代码(在自己的线程中运行)(真)循环,但似乎没有帮助。任何帮助,将不胜感激。谢谢!

+0

我相信你需要使用' HttpURLConnection'而不是'URLConnection',并且调用'setRequestMethod(“GET”)'来启动正确的HTTP通信,所以servlet容器知道调用哪个servlet的方法。 –

+3

@Jozef:不,OP只是在寻找推动力,但基本概念错了。 KS7X,你的servlet基本上是回应Java应用程序(在'response'变量中),而不是你所期望的“浏览器页面”。哪些对解决您的问题最有帮助? http://stackoverflow.com/q/25947790或http://stackoverflow.com/q/32426674? – BalusC

+0

感谢@BalusC,这两个链接都给我一个新的途径,让我的申请工作。 – KS7X

回答

-1

有两个误区:

  1. 首先,servlet的返回结果字符串不是在浏览器中,但在你的应用程序JA。
  2. 通过使用conn.getOutputStream(),您隐式设置请求方法类型为“POST”值,因此您必须在servlet中实现“doPost”方法来处理请求。

JA - 发送请求和servlet的返回响应字符串:

try { 

URL url = new URL("http://(/*ip address*/:8080/FirstServlet/myfirstservlet"); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
conn.setRequestMethod("POST"); 
conn.setDoOutput(true); 
conn.setDoInput(true); 


conn.setRequestProperty("Content-Type", "text/html"); 
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream())); 
out.write("testing"); 
out.flush(); 
out.close(); 

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 

String responsePart; 
StringBuilder resultString = new StringBuilder(); 

while ((responsePart = in.readLine()) != null) { 
    resultString.append(responsePart); 
} 
in.close(); 

System.out.println("Response from servlet: " + resultString); 

} catch (MalformedURLException ex) { 
    ex.printStackTrace(); 
} catch (IOException iex) { 
    iex.printStackTrace(); 
} 

的S - 这是由JA送回JA返回的字符串:

protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { 

    dummy(); 
    PrintWriter out = res.getWriter(); 
    BufferedReader bufferedReader = null; 
    StringBuilder stringBuilder = new StringBuilder(); 
    char test = 'a'; 

    res.setContentType("text/html"); 
    try{ 
     InputStream inputStream = req.getInputStream(); 

     if (inputStream != null) { 
      bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 

      char[] charBuffer = new char[128]; 
      int bytesRead = -1; 

      while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { 
       stringBuilder.append(charBuffer, 0, bytesRead); 

      } 

      //Return string to JA 
      out.println(stringBuilder.toString()); 

     } else { 
      stringBuilder.append(""); 
      out.println("0"); 
     } 
    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } 
} 
相关问题