2016-10-20 2 views
0

2 개의 JSP 페이지가 있습니다. GET 요청으로 1 jsp에서 다른 데이터로 데이터를 전송합니다. 이제 출력 데이터를 두 번째 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> 

여기 내 2 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 : 소켓 연결되어 있지 않습니다 : 연결'그리고 왜 이런 식으로하고있는거야? –

답변

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를이 경우에 작동하지 않습니다.

관련 문제