Craig
Craig

Reputation: 157

Reading table html rows values to a servlet to another jsp page for editing

I want to get the values for the rows which is then sent to a Java servlet then read by another page and inserts those values into the text boxes for the user to edit and write it back into the text file.

So it gets read by ProductIO which reads the textfile. Its then entered into a jsp table

   <c:forEach var="product" items="${products}">
                <tr>
                    <td ><c:out value='${product.code}'/></td>
                    <td ><c:out value='${product.description}'/></td>
                    <td >${product.priceCurrencyFormat}</td>
                    <td><form action="editproduct" method="post">
                            <input type="submit" value = "Edit">
                        </form>
                    </td>
                    <td><form action="deleteproduct" method="post">
                            <input type="submit" value = "Delete">
                        </form>
                    </td>
                </tr>
              </c:forEach>

The User Clicks either the delete or edit button which will then send action to either the deleteproduct servlet or the editproduct servlet(only asking about the edit for now)

The edit product servlet

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpSession session = request.getSession();
    String url = "/editproduct.jsp";
    getServletContext()
            .getRequestDispatcher(url)
            .forward(request, response);
    String action = request.getParameter("action");
    if (action == null) {
        action = "editproduct";  // default action
    } else if (action.equals("editproduct")) {
        String productCode = request.getParameter("productCode");
        String descString = request.getParameter("description");

        //HttpSession session = request.getSession();
        Product product = (Product) session.getAttribute("cart");
        if (product == null) {
            product = new Product();
        }
        getServletContext()
                .getRequestDispatcher(url)
                .forward(request, response);
    }
}

Which the three Values are put into three text boxes on the editProduct.jsp page(where im having a problem getting the values to insert into the text boxes for it to be written back to the text file with the new information)

            <form action="Product" method="post" >
        <input type="hidden" name="action" value="add">        
        <label>Code:</label>
        <input type="text" name="code" value='<%=request.getAttribute("code")%>' 
               required><br>
        <label >Description:</label>
        <input type="text" name="desc" value='<%=request.getAttribute("description")%>' 
               required ><br>
        <label >Price:</label>
        <input type="text" name="price" value='<%=request.getAttribute("price")%>'  
               required ><br>        
        <label>&nbsp;</label>
        <!--<input type="submit" value="Update Product" class="margin_left">-->
        <!--<input type="submit" value="View Product" class="margin_left">-->
        <button type="submit">Update</button><br>

I can share more code if needed.

Upvotes: 4

Views: 3745

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201439

You aren't calling request.setAttribute() with any of the attributes in your Servlet. I assume you meant to add something like

request.setAttribute("code", productCode);
request.setAttribute("description", descString);
request.setAttribute("price", product.getPrice());

Before forwarding the request.

Upvotes: 2

Related Questions