【问题标题】:Getting value from jsp page table从jsp页表中获取价值
【发布时间】:2017-05-12 22:16:06
【问题描述】:

我想从 jsp 页表中获取值并使用 reservations.jsp 将其插入数据库。下面的代码我可以在表格中正确打印出数据库中名为“购买”的选项部分,但我无法在表格中获得 activityId 部分。它在数据库中返回 null。在reservations.jsp 中不要读取actvityId1。我认为问题出在 activityid1 部分,代码不包括“name='buy'”这样的代码。如何获取 activityId1 值?

音乐.jsp

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <%@ page import ="java.sql.*" %>




<!DOCTYPE html>
<html>
<body background="http://www.teamarking.com/barcode/bar_background.jpg"> 
    <form method="post" action="reservations.jsp">

        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Book Ticket</title>
    </head>

    <center>    
        <table border="1" width="30%" height="30%">
            <th><font color='#D18603'>ActivityID</font>
            <th><font color='#D18603'>Type</font></th>
            <th><font color='#D18603'>Description</font></th>
            <th><font color='#D18603'>City</font></th>
            <th><font color='#D18603'>Location</font></th>
            <th><font color='#D18603'>Date</font></th>
            <th><font color='#D18603'>Price</font></th>
            <th><font color='#D18603'>Buy</font>
                <form action="some.jsp">

                    </tr>

                    <form method="post">



                        <%
                            Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
                            Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");

                            Statement st = con.createStatement();
                            ResultSet rs;
                            rs = st.executeQuery("select * from activities where type='müzik'");
                            while (rs.next()) {

                                String activityid1 = rs.getString("id");
                                String type1 = rs.getString("type");
                                String description1 = rs.getString("description");
                                String city1 = rs.getString("city");
                                String location1 = rs.getString("location");
                                String date1 = rs.getString("date");
                                String price1 = rs.getString("price");

                                out.println("<tr>");
                                out.println("<td>" + activityid1 + "</td>");
                                out.println("<td>" + type1 + "</td>");
                                out.println("<td>" + description1 + "</td>");
                                out.println("<td>" + city1 + "</td>");
                                out.println("<td>" + location1 + "</td>");
                                out.println("<td>" + date1 + "</td>");
                                out.println("<td>" + price1 + "</td>");
                                out.println("<td><b><form action='reservations.jsp'><select name='buy'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select><input type='submit' value='Submit'></form></b>");
                                out.println("</tr>");

                            }
                            st.close();

                        %>


                        </center>
                        </table>
                        <tr>
                        <td><input type="reset" value="Reset" /></td>
                        </tr> 
                    </form>
                    <br><br><a href='logout.jsp'>Log out</a>
                </form>
                </body>
                </html>

reservations.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import ="java.sql.*" %>
<%
request.getParameter("activityid1");
request.getParameter("buy");
String username = (String) request.getSession().getAttribute("username");

Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:derby://localhost:1527/users", "users", "123");

String sorgu = "INSERT INTO reservation(id,username,buy) VALUES ('" + activityid1 + "', '" + username + "','" + request.getParameter("buy") + "') ";

java.sql.Statement st = con.createStatement();

int rowNum = st.executeUpdate(sorgu);
response.sendRedirect("paypal.html");
st.close();
%>

【问题讨论】:

  • 您在哪里创建buy 按钮输入?是表格的submit吗?
  • 可能与您的问题无关,但代码易受 SQL 注入攻击。请改用PreparedStatement
  • 在第一个代码中。我会做的,但这现在更重要:)

标签: java html mysql sql jsp


【解决方案1】:

activityid1 不是表单的一部分。它的值不会随请求一起发送。您需要将其添加为表单的隐藏元素:

out.println("<td><b><form action='reservations.jsp'><select name='buy'><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option><option value='4'>4</option><option value='5'>5</option></select><input type='submit' value='Submit'>  <input type=\"hidden\" id=\"activityid1Value\" name=\"activityid1Hidden\" value=\""+activityid1 +"\"></form></b>");

然后检索它:

request.getParameter("activityid1Hidden");

希望能帮到你

【讨论】:

    猜你喜欢
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    • 1970-01-01
    相关资源
    最近更新 更多