会话servlet不适用于下拉菜单jsp

时间:2016-12-22 19:20:35

标签: java jsp servlets

这是我对order.jsp的编码,这是客户插入订单的表单,我使用session.menus在数据库的HTML输入元素中显示选项菜单。我已经使用jdbcutility.java连接到数据库,在那里我将sql语句连接到我的servlet与数据库并且它用于登录但不用于此会话。

<form class="form-horizontal" method="post" action="InsertOrderServlet">


<center>
    <table border="10" cellpadding="20" cellspacing="20">
        <thead>
            <tr>
                <th colspan="2"><h2 style="line-height: 3;" >Order Food</h2></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td><br><br>Food</br></br></td>
                <td>
                    <br><br>
                    <div class="col-lg-9">
                        <select class="form-control" name="menuName">
                            <c:forEach items="${sessionScope.menus}" var="currentmenu" varStatus="loop">
                                <option><c:out value="${currentmenu.foodName}" /></option>
                            </c:forEach>
                        </select>
                    </div>
                </td>
            </tr>
            <tr>
                <td>Quantity</td>
                <td><input type="text" name="quantity" required/></td>
            </tr>
            <tr style="line-height: 10;" >

                    <td> <button  class="btn btn-primary" type="reset" >Cancel</button></td>
               <td > <button type="submit" class="btn bt primary">Submit</button></td>

                </td>                        
           </tr> 

        </tbody>

    </table>
    </center>
</form>

getMenuServlet.java

  //select all from menu
    try {                    
        ResultSet rs = jdbcUtility.getPSSelectMenu().executeQuery();

        while (rs.next()) {       

            menu = new Menu();
            menu.setId(rs.getInt("menuid"));
            menu.setfoodName(rs.getString("foodName"));
            menu.setImage(rs.getString("image"));           
            menu.setPrice(rs.getDouble("price"));

            //put into arraylist
            menus.add(menu);
        }
    }
    catch (SQLException ex) 
    {            
    }               

    //put into sessions
    session.setAttribute("menus", menus);

jdbcutility.java

 String sqlValidateLogin = "Select username,password from customer where username=? and password=?";
        psInsertLogin = con.prepareStatement(sqlValidateLogin);

        String sqlInsertOrder = "INSERT INTO orderfood(menuName, quantity) " + "VALUES(?, ?)";
        psInsertOrder = con.prepareStatement(sqlInsertOrder);

        String sqlSelectAllMenu ="SELECT * FROM menu";   
        psSelectAllMenu = con.prepareStatement(sqlSelectAllMenu);

        String sqlSelectAllOrder ="SELECT * FROM orderfood";   
        psSelectAllOrder = con.prepareStatement(sqlSelectAllOrder);

这种情况一直发生,我该如何解决这个问题?

image blank

1 个答案:

答案 0 :(得分:0)

确保JSP是允许访问会话。

<%@ page session="true" %>

要使用核心JSTL,请确保包含以下代码。

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>