Doubts - JSP - Servlets

Asked

Viewed 38 times

0

Good morning, you guys, I’m having difficulties to perform the CRUD in a test app....

I made it with the default (almost) MVC, the problem is that by clicking on the form of "New Product" and filling in the fields it does not insert, just directs me to a screen with the name of the Controller in the url (getting: http://localhost:8080/Test/Productocontroller)

This is the Cód of my form:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>ADD novo Produto</title>
    </head>
    <body>
        <form method="POST" action='ProdutoController' name="formAddProduto">
            <input type="hidden" name="action" value="insert" />

            <table>

                <tr>
                    <td>Codigo de Barras</td>
                    <td><input type="text" name="codBarras" />        
                </tr>

                <tr>
                    <td>Nome do produto</td>
                    <td><input type="text" name="Nome_Produto" />        
                </tr>

                <tr>
                    <td>Valor</td>
                    <td><input type="text" name="Valor_Produto" />        
                </tr>

                <tr>
                    <td></td>
                    <td><input type="submit" value="submit" /></td>
                </tr>          
            </table>
        </form>
        <p><a href="ProdutoController?action=listProduto"> Todos os Produtos </a></p>
    </body>
</html>

This is the Controller snippet that belongs to INSERT:

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

    String redirect="";
    String codBarras = request.getParameter("codBarras");        
    String action = request.getParameter("action");

    if(!((codBarras) == null) && action.equalsIgnoreCase("insert"))
    {
        int cb = Integer.parseInt("codBarras");

        Produto p = new Produto();
        p.setCodBarras(cb);
        p.setNmProduto(request.getParameter("Nome_Produto"));
        p.setValorProduto(Float.parseFloat(request.getParameter("Valor_Produto")));

        dao.addProduto(p);

        redirect = ListaProduto;

        request.setAttribute("produtos", dao.getProdutos());  

        System.out.println("Produto adicionado com sucesso");
    }

I’m having this same problem with the Update form.... occurs the same thing already the others (list Product and Delete Product) are normal.

Code of the update form Product:

<html>
<head>      
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Editar Produto</title>
</head>
<body>
    <% Produto p = new Produto(); %>
    <% ProdutoDAO dao = new ProdutoDAO(); %>

    <form method="POST" action='ProdutoController' name="formEditaProduto">
        <input type="hidden" name="action" value="edit" />
        <% String cb = request.getParameter("codBarras");
        if (!((cb) == null )){
            int cb2 = Integer.parseInt(cb);
            p = dao.getCodBarras(cb2);
         %>

        <table>
             <tr>
                <td>Codigo de Barras</td>
                <td><input type="text" name="codBarras" readonly="readonly" value="<%=p.getCodBarras()%>"> </td>        
            </tr>

            <tr>
                <td>Nome do produto</td>
                <td><input type="text" name="Nome_Produto" value="<%=p.getNmProduto()%>"/>        
            </tr>

            <tr>
                <td>Valor</td>
                <td><input type="text" name="Valor_Produto" value="<%=p.getValorProduto()%>"/>        
            </tr>

            <tr>
                <td></td>
                <td><input type="submit" value="Update" /></td>
            </tr>          
        </table>
                <% } else{ 
                        out.println("ID Not found");
                } %>

    </form>
</body>

And the Controller code for Edit:

else if (action.equalsIgnoreCase("edit")){

        String codBarras2 = request.getParameter("codBarras");

        int cb = Integer.parseInt(codBarras2);            
        Produto p = new Produto();
        p.setCodBarras(cb);
        p.setNmProduto(request.getParameter("Nome_Produto"));
        p.setValorProduto(Float.parseFloat(request.getParameter("Valor_Produto")));

        dao.uptProduto(p);

        request.setAttribute("produto", p);
        redirect = ListaProduto;
        System.out.println("Record updated Successfully");

1 answer

0

The operations for Insert and update are of type POST in your forms. Therefore, the method in Servlet that should receive them is the doPost() and not the doGet(). Just change the method that should work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.