0

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

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

Below is my 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 page in which I need to retrieve the value from first.jsp and make a select query and return the result back..

<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?

codingrose
  • 15,563
  • 11
  • 39
  • 58
AKIWEB
  • 19,008
  • 67
  • 180
  • 294

2 Answers2

2

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

$.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

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());
}
%>
Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
  • Thanks Justin.. I am able to make the call correctly to second.jsp page from my first.jsp page using your suggestion.. But as you see in my second.jsp page, I am making a SQL select query against the database. So it should return me back the SELECT query response in first.jsp inside data variable? Correct? If yes, then it is not happening. It is returning me back the full html code not the data from the database. Any idea what's wrong? – AKIWEB Jan 11 '14 at 22:16
  • If you want to return a result of select query, consider using JSON to send back data. Doing so, you can get back all data in jquery (first.jsp) – Justin Iurman Jan 11 '14 at 22:19
  • Thanks Justin. Can you provide an example on this as well corresponding to my example as in the question? I recently started working with this so have no clue what changes I am supposed to make? Any help will be appreciated. – AKIWEB Jan 11 '14 at 22:20
  • Thanks.. Now it is printing out on the console but it is not returning it back to `first.jsp` page in the data variable? Any idea what's wrong? Meaning alert box is not getting popped up now which has data variable – AKIWEB Jan 11 '14 at 22:39
  • Whoops, my bad, now it's on the page – Justin Iurman Jan 11 '14 at 22:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/45027/discussion-between-akiiddweeber-and-justin-iurman) – AKIWEB Jan 11 '14 at 22:49
0

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.

BrianC
  • 1,793
  • 1
  • 18
  • 26