Java 将值从一个 JSP 页面传递到另一个 JSP 页面并从第一个 JSP 获取响应数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21068389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:21:54  来源:igfitidea点击:

Pass value from one JSP page to another JSP page and get response data back from First JSP

javajqueryhtmljspjstl

提问by AKIWEB

Below is my first.jspfrom which I am supposed to call second.jsppage using AJAX... And I need to pass a value from first.jsppage to second.jsppage.

下面是我first.jsp应该second.jsp使用 AJAX调用页面的我......而且我需要在first.jsp页面之间传递一个值second.jsp

And then in the second.jsppage use that variable value and make a SELECT query using that and return the data back to first.jsp page

然后在second.jsp页面中使用该变量值并使用该值进行 SELECT 查询并将数据返回给first.jsp page

Below is my first.jsp page

下面是我的 first.jsp page

<html>
    <head>
    </head>
    <body>    
        <p><input type="radio" name="dish" value="Indian" id="radioButton"> Continental</input></p>
        <p><label for="male" id="columnData">Male</label></p>    
        <script>
            $(document).ready(function() {
                $('#radioButton').click(function() {
                    alert($('#columnData').html());
                    var name = $('#columnData').html();             
                    $.ajax({  
                        type:"POST",      
                        url: "second.jsp",  
                        data:"name=" +name,           
                        success: function(success) {                                  
                        }  
                    });                 
                });
            });
        </script>
    </body>
</html>

Below is my second.jsp pagein which I need to retrieve the value from first.jspand make a select query and return the result back..

下面是我second.jsp page需要从中检索值first.jsp并进行选择查询并将结果返回的我。

<html>
    <head>
        <title>SELECT Operation</title>
    </head>
    <body>
    <sql:setDataSource var="snapshot" driver="org.postgresql.Driver"
                       url="jdbc:postgresql://localhost/postDB"
                       user="postgres"  password="hello"/>

    <sql:query dataSource="${snapshot}" var="result">
        // use the name variable value here passed from first.jsp page?
        SELECT * from Employees where name = ?;
    </sql:query>
</body>
</html>

I am not sure how to pass the value from one JSP page to another JSP page and then return back the result from second.jsp page to first.jsp page?

我不确定如何将值从一个 JSP 页面传递到另一个 JSP 页面,然后将结果从 second.jsp 页面返回到 first.jsp 页面?

采纳答案by Justin Iurman

In your first.jsp file, try using $.post instead (more appropriate).

在您的 first.jsp 文件中,尝试使用 $.post 代替(更合适)。

$.post("second.jsp", {'name': name}, 
       function(data) 
       { 
          alert("Result from second.jsp: " + data.name + " " + data.type); 
       }
);

In your second.jsp file, you can now get "name" variable like this

在您的 second.jsp 文件中,您现在可以像这样获得“名称”变量

request.getParameter("name")

Then, do your query and return the result

然后,执行查询并返回结果

<%@page import="org.json.simple.JSONObject"%>

<%
if (request.getParameter("name") != null)
{
   response.setContentType("application/json");

   ... your select query ...

   JSONObject json = new JSONObject();
   ... put your sql data like this ...
   json.put("name", "hello");
   json.put("type", "world");

   response.getWriter().write(json.toString());
}
%>

回答by BrianC

So you have created your client side interfaces, have you created the server side logic to process those pages?

所以您已经创建了客户端接口,是否创建了服务器端逻辑来处理这些页面?

You have to have server side logic to receive the page requests and respond with your JSP pages. As well as to respond you AJAX calls. You will need to create Servlets, or setup one of the web app frameworks like Spring WebMVC, JSF, Struts, etc.

您必须具有服务器端逻辑来接收页面请求并使用 JSP 页面进行响应。以及响应您的 AJAX 调用。您将需要创建 Servlet,或设置 Web 应用程序框架之一,如 Spring WebMVC、JSF、Struts 等。