2016-10-20 10 views
0

私は2つのjspページを持っています。 GETリクエストとして1 jspから別のデータにデータを送信しています。 出力データを2番目のJSPに表示したいのですが、できません。jspからGETコールとしてデータを受信する方法

は、ここで私の第一のjspすなわちindex.jspを

<%-- 
    Document : Spago 
    Created on : 14 Oct, 2016, 2:06:12 PM 
    Author  : ndoshi 
--%> 

<%@page import="java.io.InputStreamReader"%> 
<%@page import="java.io.BufferedReader"%> 
<%@page import="java.net.*"%> 
<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
     <script> 
      setTimeout(function() { 
       document.location = "http://localhost:8080/demo/index.jsp"; 
      }, 60000); // <-- this is the delay in milliseconds 
     </script> 
    </head> 
    <body> 
     <% 
      try { 
       String machine = InetAddress.getLocalHost().getCanonicalHostName(); 
       String user="Niket"; 
       URL url = new URL("http://localhost:8080/demo/db.jsp?machine=" + machine+"&user="+user); 
       HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
       conn.setRequestMethod("GET"); 
       conn.setRequestProperty("Accept", "application/json"); 

       BufferedReader in = new BufferedReader(
       new InputStreamReader(conn.getInputStream())); 
       String inputLine; 
       StringBuffer response1 = new StringBuffer(); 

       while ((inputLine = in.readLine()) != null) { 
        response1.append(inputLine); 
       } 

       System.out.println(inputLine); 

       out.println("Index : "+inputLine); 
       in.close(); 

       if (conn.getResponseCode() != 200) { 

       } 
      } catch (Exception e) { 
      } 
     %> 
    </body> 
</html> 

ここにある私の第二のjspすなわちdb.jspある

<%-- 
    Document : db.jsp 
    Created on : 20 Oct, 2016, 1:29:47 PM 
    Author  : ndoshi 
--%> 

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <% 
      out.println("Response : "+request.getParameter("machine")+","+request.getParameter("user")); 
     %> 
    </body> 
</html> 
+0

最初のJSPで例外が表示されました。 'catch(Exception e){System.out.println(e);}' –

+0

私は追加しましたが、エラーはスローされません。 –

+0

あなたのコードを試しました。例外は 'java.net.SocketException:ソケットは接続されていません:connect'を持っています。なぜこのようにしていますか? –

答えて

1

あなたは新しいもので、次のコードを変更する必要があります。

旧コード:

String inputLine; 
       StringBuffer response1 = new StringBuffer(); 

       while ((inputLine = in.readLine()) != null) { 
        response1.append(inputLine); 
       } 

       System.out.println(inputLine); 

新しいコード

String inputLine; 
String Line=""; 
while ((inputLine = in.readLine()) != null) { 
    Line+=inputLine; 
} 
out.println(Line); 

StringBufferのは、この場合には動作しません。

関連する問題