从jsp页表获取价值

时间:2016-12-28 15:42:36

标签: java html mysql sql jsp

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

music.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();
%>

1 个答案:

答案 0 :(得分:0)

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");

希望我帮助你