【问题标题】:how to sent data by clicking a link in jsp如何通过点击jsp中的链接发送数据
【发布时间】:2014-04-11 07:09:29
【问题描述】:

我想从db 获取数据并显示在table 中,并且每条记录都应该有一个See Details 的链接,为此我有:

<form name="submitForm" method="POST" action="details.jsp">
    <table border="1" width="80%">
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
            <th>More</th>
        </tr>

        <%
            Connection connection = null;
            Statement statement = null;
            ResultSet resultSet = null;
            try {
                connection = com.secure.db.DatabaseManager.getConnection();
                statement = connection.createStatement();
                resultSet = statement.executeQuery("SELECT * from address");
                while (resultSet.next()) {
        %>
        <tr>
            <td><%=resultSet.getString("first_name")%> <input type="hidden"
                name="fname" value="<%=resultSet.getString("first_name")%>"></td>
            <td><%=resultSet.getString("last_name")%> <input type="hidden"
                name="lname" value="<%=resultSet.getString("last_name")%>"></td>
            <td>    <a href="javascript:document.submitForm.submit()">See Details</a></td>
        </tr>
        <%
            }
        %>

    </table>

    <%
    } catch (Exception ex) {
        ex.printStackTrace();
%>
    Ooops, something bad happened:
    <%
    } finally {
        if (resultSet != null)
            resultSet.close();
        if (statement != null)
            statement.close();
        if (connection != null)
            connection.close();
    }
%>
</form>

现在结果是:

当我点击第一条记录时,我得到了正确的信息,但是当我点击第一条记录以外的信息时 我仍在获取第一条记录的数据。如何获取正确的数据?

【问题讨论】:

  • 我没有看清楚你的要求,请说清楚。
  • @JqueryLearner 请看我的问题我已经编辑了这个...

标签: java javascript jquery jsp tomcat


【解决方案1】:

试试这个代码

        <table border="1" width="80%">
            <tr>
                <th>First Name</th>
                <th>Last Name</th>
<th>&nbsp;</th>
            </tr>

            <%
                Connection connection = null;
                Statement statement = null;
                ResultSet resultSet = null;
                try {
                    connection = com.secure.db.DatabaseManager.getConnection();
                    statement = connection.createStatement();
                    resultSet = statement.executeQuery("SELECT * from person");
                    int iCount=0;


 while (resultSet.next()) {
            iCount =iCount+1;
                %>
                <tr>
                    <td colspan="3">
     <form name='submitForm_<%=iCount%>' method="POST" action="details.jsp">
    <table><tbody><tr><td>

    <input type="hidden" name="first_name"
                        value='<%=resultSet.getString("first_name")%>'><%=resultSet.getString("first_name")%></td>
                    <td><input type=="hidden" name="last_name"
                        value='<%=resultSet.getString("last_name")%>'><%=resultSet.getString("last_name")%></td>
                   <td><a href="javascript:document.submitForm_<%=iCount%>.submit()">Click Me</a><td>
                </tr>
    </tbody></table> </form></td>
            <%
                }
            %>

        </table>

    <%
        } catch (Exception ex) {
            ex.printStackTrace();
    %>
    Ooops, something bad happened:
    <%
        } finally {
            if (resultSet != null)
                resultSet.close();
            if (statement != null)
                statement.close();
            if (connection != null)
                connection.close();
        }
    %>

【讨论】:

  • 当您提交表单时,您将在服务器端获得多个 first_name 和 last_name。不要使用 request.getParamether("first_name") 用户 request.getParameterValues("first_name") 这将为您提供 first_name 数组。
  • OK 那么如何获取其他值呢?
  • 尝试编辑后的代码并使用旧的 request.getParameter("first_name") 代码。
【解决方案2】:

从附图中我可以看到你可以看到名字和姓氏的值。这是因为你的输入类型是隐藏的

<input type="hidden" name="param1"
                value="<%=resultSet.getString("first_name")%>"></td>

你在这里也说过 *现在我希望 first_name 和 last_name 应该显示为链接,我找到了这个讨论,所以我尝试了:*

所以你应该这样做

<a href="your _link"><%=resultSet.getString("first_name")%></a>

【讨论】:

  • @SuperUser 在原始帖子中,您说过要输入名字和姓氏,但现在您正在将详细信息作为链接查看
  • @JqueryLearner 我不确定,但通过访问您的个人资料,我猜您是 Down vote 这个 Q 的人,但要成为 civil 并记住他研究并更改了他的 @ 987654325@,他在问如何发送data by clicking a link,所以他的问题是询问原始或编辑。我认为这是一个很好的问题!
  • 好吧,如果你的前辈然后do you have all the power to close Vote, Down Vote和其他一些东西,这很奇怪不是吗??
  • @user3310291 不,它并不奇怪。投票,关闭和所有东西都是由社区而不是一个人决定的。你应该首先了解什么是stackoverflow
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 2015-11-08
  • 1970-01-01
  • 2012-08-21
  • 2015-03-15
相关资源
最近更新 更多