简体   繁体   English

单击将锚标记的值传递给另一个文件

[英]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.我试图从我的数据库中打印数据作为锚标记,并在单击它时将值(在 rs.getString(1) 中)传递给下一个 jsp 文件(demo.jsp)。 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.我试图在文件 demo.jsp 中创建一个会话并从中检索值,但它总是打印数据库中的最后一个值。 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.我能够打印给定文件中的数据,但是将值传递给 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然后通过使用在 demo.jsp 中获取它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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