0

我有一个 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);
    }

汽车评论.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>
4

1 回答 1

0

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

request.getSession().getAttribute("value")
于 2013-09-09T09:56:00.373 回答