获取从servlet到jsp的字符串列表

时间:2013-09-09 07:46:59

标签: java html jsp servlets

我有一个JSP页面,其中有两个标签。首先,我正在尝试输入诸如Tata,Hyundai,Toyota,Audi等汽车制造商名称。当用户首先选择任何选项时,它应该显示来自该制造商的车型,如Innova,Land Cruiser等。所以当用户选择第一个标签中的任何选项,我正在调用一个servlet,它从列表中的数据库中获取所有模型,并将列表设置为会话属性并将请求转发回JSP。但是在jsp中,当我尝试获取列表时,它会给出NULL POINTER EXCEPTION。怎么解决? 代码如下:

DbReviewCar.java

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    Connection conn= null;
    PreparedStatement pstmt= null;
    ResultSet rs;
    String sql= null;
    String maker= request.getParameter("make");
    List modellist= new ArrayList();
    /*if(maker==null)
    {
        modellist.add("ferrari");
        modellist.add("hummer");
         request.getSession().setAttribute("value", modellist);
         request.getRequestDispatcher("CarReview.jsp").forward(request,response);


    }
    else
    {*/


    try {
        Class.forName("com.mysql.jdbc.Driver");
        conn= DriverManager.getConnection("jdbc:mysql://localhost/cardetails", "root", "Welcome123");
        sql= "select model from cars where make=?;";
        pstmt= conn.prepareStatement(sql);
        pstmt.setString(1, maker);
        rs= pstmt.executeQuery();
        while(rs.next())
        {
            String mod= rs.getString(1);
            modellist.add(mod);
            System.out.println(mod+">>>>>>>>>>>>>>>>>>.");
        }


    } catch (ClassNotFoundException e) {

        e.printStackTrace();
    } catch (SQLException e) {

        e.printStackTrace();
    }


     request.getSession().setAttribute("value", modellist);
     request.getRequestDispatcher("CarReview.jsp").forward(request,response);
    }

CarReview.jsp

这是我的JSP文件

<form action="DbReviewCar" method="get" name="myform">
    <table>
        <tr>
            <td>
        <tr>
            <td>Make:</td>
            <td><select name="make" onchange="this.form.submit()"><option>select</option>
                    <option>Maruti</option>
                    <option>Ford</option>
                    <option>Honda</option>
                    <option>Skoda</option>
                    <option>Tata</option>
                    <option>Audi</option>
                    <option>Toyota</option></select><br></br></td>
        </tr>


        <%
            List list = new ArrayList();
            list.addAll((List) (request.getSession().getAttribute("value")));
        %>



        <tr>
            <td>Model:</td>
            <td><select name="model">


                    <%
                        for (int i = 0; i < list.size(); i++) {
                    %>
                    <option value=<%=list.get(i)%>><%=list.get(i)%></option>
                    <%
                        }
                    %>
            </select><br></br></td>
        </tr>

        <tr>
            <td>Rating For Style:</td>
            <td><input type="text" name="style"><br></br></td>
        </tr>
        <tr>
            <td>Rating for comfort:</td>
            <td><input type="text" name="comfort"><br></br></td>
        </tr>
        <tr>
            <td>Rating for Performance:</td>
            <td><input type="text" name="performance"><br></br></td>
        </tr>
        <tr>
            <td>Rating for FuelEconomy:</td>
            <td><input type="text" name="economy"><br></br></td>
        </tr>
        <tr>
            <td>Review:</td>
            <td><textarea cols="18" rows="3"></textarea><br></br></td>
        </tr>

        <tr>
            <td><Button>Save</Button></td>
            <td><input type="reset" name="cancel" value="Cancel" /></td>
        </tr>






    </table>


</form>

1 个答案:

答案 0 :(得分:0)

第一次加载jsp时,未设置“value”属性。 尝试检查null的值:

request.getSession().getAttribute("value")