gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:21:18 25 4
gpt4 key购买 nike

我有2个jsp页面。我正在将数据作为 GET 请求从 1 个 jsp 发送到另一个 jsp。现在我想在第二个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 在这种情况下不起作用。

关于java - 如何通过 GET 调用从 jsp 接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40149663/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com