简体   繁体   中英

on clicking pass value of anchor tag to another file

I am trying to print the data from my database as anchor tags and pass the value(in rs.getString(1)) to next jsp file(demo.jsp) on clicking it. I tried to make a session and retrieve the values from it, in file demo.jsp but it is always printing the last values in database. I know its because of session being replaced everytime the loop runs. I am able to print the data in the given file, but problem is occuring to pass the value to demo.jsp. Is there any alternative way of doing this

                    <%
                      try
                     {   
String cars2=request.getParameter("cars");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@Amit-PC:1521:XE","system","tiger");
PreparedStatement st=con.prepareStatement("select thread from thread_all where car=?");
        st.setString(1,cars2);
        ResultSet rs;
        rs=st.executeQuery();
        while(rs.next())
        {
        %>

            <div class="rows">
                <div class="col-lg-12 well">
                <%  session.setAttribute("posttopic",rs.getString(1));   %>
                  <a href="demo.jsp">asked :<%out.print(rs.getString(1));%>
                    </a>
                    <br>
            </div>
         </div>
    <%
        }
        }
        catch(Exception e)
        {
        }
    %>

Try storing the data in a collection and then store the collection object in session. Then display the data in the next page using this collection object.

I found the solution for my own question. I did this by passing the value in anchor tag itself.

<a href="demo.jsp?id1=<%=rs.getString(2) %>">><%=rs.getString(1)%></a>

and then fetching it in demo.jsp by using

String var=request.getParameter("id1");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM