Mr x
Mr x

Reputation: 866

How to receive data from jsp as GET Call

I have 2 jsp pages. I am sending data from 1 jsp to another as GET request. Now I want to display output data in the 2nd jsp but I am not able to do it.

here is my 1st jsp i.e 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>

Here is my 2nd jsp i.e 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>

Upvotes: 0

Views: 110

Answers (1)

NIket
NIket

Reputation: 924

You need to modify the following code with new one.

Old code :

String inputLine;
                StringBuffer response1 = new StringBuffer();

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

                System.out.println(inputLine);

New Code

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

StringBuffer does not work in this case.

Upvotes: 1

Related Questions