how to pass data form jps to servlet?

Almamun :

I am working on simple CRUD example project in jsp and servlet. But I am unable to pass data from jsp to servlet through URL < a href="servletName" value="<%= id.getid()" name="id">Delete < / a>. and in servlet page i am trying to get that value through int id = Integer.parseInt(request.getParameeter("id")). But i am getting a null value. below is my jsp and servlet code.

                         <tbody>
                            <%
                                for (books b:book){
                            %>
                            <tr>
                                <th scope="row"><%= b.getId() %></th>
                                <td><%= b.getBookName() %></td>
                                <td><%= b.getBookDesc() %></td>
                                <td><%= b.getAuthName() %></td>
                                 <td><%= b.getCat() %></td>
                                 <td><a href="editbook.jsp?id=<%= b.getId() %>">Edit</a><a href="DeleteBookServlet" name="deleteId" value="<%= b.getId() %>">Delete</a></td>
                                 <!--data-toggle="modal" data-target="#editBooks"-->
                            </tr>
                            <%
                                }
                            %>
                        </tbody>

Servlet Code

        String id  = request.getParameter("deleteId");
        int bid = Integer.parseInt(id);

        try{

            BooksDao bkd = new BooksDao(ConnectionDao.getCon());
            bkd.deleteBook(bid);
            response.sendRedirect("index.jsp");
        }catch(Exception e){
            e.printStackTrace();
        }
Swati :

You have not pass your value in your <a> tag to servlet that the reason its returning null . Instead do like below :

<a href="DeleteBookServlet?deleteId=<%=b.getId()%>" name="deleteId">Delete</a>

and get it in servlet using

int id = Integer.parseInt(request.getParameter("deleteId"));

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=18649&siteId=1