简体   繁体   中英

Dynamic Field with JSP and HTML

<form id="send" action="doaddnewuser.jsp" method="post">
                <div id="controls" style="background-color:#FFFFFF;height:500px;width:750px;float:left;">
                    <table>
                        <thead>
                            <tr>
                                <th></th>
                                <th></th>
                            </tr>
                        </thead>
                        <tbody>

                            <%
                                String buffer = "<tr><td><label for=''" + "productid" + "''>Product ID *</label></td> <td><select name='" + "productid" + "'>";
                                try {
                                    Class.forName("oracle.jdbc.OracleDriver");
                                    Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "*****", "*****");

                                    Statement stmt = con.createStatement();
                                    ResultSet rs = stmt.executeQuery("Select * from product");
                                    while (rs.next()) {
                                        buffer = buffer + "<option value='" + rs.getString(1) + "'>" + rs.getString(1) + "</option>";
                                    }
                                    buffer = buffer + "</select> </td></tr>";
                                    response.getWriter().println(buffer);
                                } catch (Exception e) {
                                    System.out.println(e);
                                }
                            %>

                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr>
                                <td><label for="price">Price *</label></td>
                                <td><input type="text" name="price" value="" size="60"/></td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr>
                                <td><label for="debitaccount">Debit Account *</label></td>
                                <td><input type="text" name="debitaccount" value="" size="25" /></td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr>
                                <td><label for="creditaccount">Credit Account *</label>
                                <td><input type="text" name="creditaccount" value="" size="25" /></td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr>
                                <td><label for="quantity">Quantity *</label></td>
                                <td><input type="text" name="quantity" value="1" size="25" /></td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr>
                                <td><label for="transactiondate">Transaction Date *</label></td>
                                <td><input type="date" name="transactiondate" value="" /></td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr>
                                <td><label for="totalprice">Total Price *</label></td>
                                <td><input type="text" name="totalprice" value="" /></td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>

                            <tr>
                                <td><label for="vat" style="text-align:justify" >15% VAT</label></td>
                                <td><input type="text" name="vat" value="" /></td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr> 
                                <td>   </td>
                                <td>   </td>
                            </tr>
                            <tr>
                                <td></td>
                                <td> 
                                    <input id ="submit" type="submit" value="Save" name="submit" />

                                </td>
                            </tr>

                        </tbody>
                    </table>                        

                </div>
            </form>

The above is my HTML and JSP code to construct sales input form - but as the page runs the product id field which is constructed by the JSP goes to the left top corner instead of being shown with the other controls

can you please help me why it wanders off.

PS the idea is to populate the product_id field from database

Dont use response.getWriter().print(..) inside the jsp, do the code below instead.

     <%
                                    String buffer = "<tr><td><label for=''" + "productid" + "''>Product ID *</label></td> <td><select name='" + "productid" + "'>";
                                    try {
                                        Class.forName("oracle.jdbc.OracleDriver");
                                        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "*****", "*****");

                                        Statement stmt = con.createStatement();
                                        ResultSet rs = stmt.executeQuery("Select * from product");
                                        while (rs.next()) {
                                            buffer = buffer + "<option value='" + rs.getString(1) + "'>" + rs.getString(1) + "</option>";
                                        }
                                        buffer = buffer + "</select> </td></tr>";
                                    } catch (Exception e) {
                                        System.out.println(e);
                                    }
                                %>
<%=buffer%>

The <%=buffer%> will print the content of the buffer variable in the page.

Then I suggest you to do this instead:

<form id="send" action="doaddnewuser.jsp" method="post">
            <div id="controls" style="background-color:#FFFFFF;height:500px;width:750px;float:left;">
                <table>
                    <thead>
                        <tr>
                            <th></th>
                            <th></th>
                        </tr>
                    </thead>
                    <tbody>
                          <tr>
                          <td>
                                <label for=''" + "productid" + "''>Product ID *</label></td> <td><select name='" + "productid" + "'>

                        <%

                            try {
                                Class.forName("oracle.jdbc.OracleDriver");
                                Connection con = DriverManager.getConnection("jdbc:oracle:thin:@//localhost:1521/XE", "*****", "*****");

                                Statement stmt = con.createStatement();
                                ResultSet rs = stmt.executeQuery("Select * from product");
                                while (rs.next()) {
                        %>
                                 <option value = "<%=rs.getString(1)%>"> <%=rs.getString(1)%> </option>
                        <%

                                }
                            } catch (Exception e) {
                                System.out.println(e);
                            }
                        %>

                                </select> </td></tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr>
                            <td><label for="price">Price *</label></td>
                            <td><input type="text" name="price" value="" size="60"/></td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr>
                            <td><label for="debitaccount">Debit Account *</label></td>
                            <td><input type="text" name="debitaccount" value="" size="25" /></td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr>
                            <td><label for="creditaccount">Credit Account *</label>
                            <td><input type="text" name="creditaccount" value="" size="25" /></td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr>
                            <td><label for="quantity">Quantity *</label></td>
                            <td><input type="text" name="quantity" value="1" size="25" /></td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr>
                            <td><label for="transactiondate">Transaction Date *</label></td>
                            <td><input type="date" name="transactiondate" value="" /></td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr>
                            <td><label for="totalprice">Total Price *</label></td>
                            <td><input type="text" name="totalprice" value="" /></td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>

                        <tr>
                            <td><label for="vat" style="text-align:justify" >15% VAT</label></td>
                            <td><input type="text" name="vat" value="" /></td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr> 
                            <td>   </td>
                            <td>   </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td> 
                                <input id ="submit" type="submit" value="Save" name="submit" />

                            </td>
                        </tr>

                    </tbody>
                </table>                        

            </div>
        </form>

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