简体   繁体   中英

Getting list of strings from servlet to jsp

I have a JSP page in which I have two tags. In first I am trying get input such as Car Maker name such as Tata, Hyundai, Toyota, Audi etc. When user selects any option in first , it should display car models from that maker such as Innova,Land Cruiser etc. So when user selects any option in first tag, I am calling a servlet which gets all the models from database in a list and setting the list as attribute of session and forwarding the request back to JSP. But in jsp when I try to fetch the list it is giving NULL POINTER EXCEPTION. How to solve it? The code is as below:

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

Here is my JSP file

<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>

When jsp loading for the first time the "value" atribute is not set. Try to check null for value:

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

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