如何通过 GET 调用从 jsp 接收数据

How to receive data from jsp as GET Call

我有 2 jsp 页。 我将数据从 1 jsp 作为 GET 请求发送到另一个。 现在我想在第二个 jsp 中显示输出数据,但我做不到。

这是我的第一个 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>

您需要将以下代码修改为新的。

旧代码:

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 在这种情况下不起作用。