Cadastro no BD com JSP

2 respostas
B

Estou com uma dúvida ao cadastrar os dados no BD. Quando envio o form ele retorna o erro 404 na página CadastroProduto.

Form JSP:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Cadastro de Produtos</title>
    </head>
    <body>
        <fieldset>
            <legend>Cadastro de Produtos</legend>
            <form action="CadastroProduto" method="POST">
                <table>
                    <tr>
                        <td>Nome:</td>
                        <td><input type="text" name="nome"/></td>
                    </tr>
                    <tr>
                        <td>Descrição:</td>
                        <td><textarea name="descricao"/></textarea></td>
                    </tr>
                    <tr>
                        <td>Código de Barras:</td>
                        <td><input type="text" name="codBarras"/></td>
                    </tr>
                    <tr>
                        <td>Categoria:</td>
                        <td><input type="text" name="categoria"/></td>
                    </tr>
                    <tr>
                        <td>Valor:</td>
                        <td><input type="text" name="valor"/></td>
                    </tr>
                    <tr>
                        <td>Quantidade:</td>
                        <td><input type="text" name="quantidade"/></td>
                    </tr>
                    <tr>
                        <td colspan="2" align="center"><input type="submit"/><td>
                        
                    </tr>
                </table>

            </form>
        </fieldset>
    </body>
</html>

CadastroProduto.java:

package br.com.estoque;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class CadastroProduto extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Produto p = new Produto();
        ProdutoDAO dao = new ProdutoDAO();
        
        p.setNome(request.getParameter("nome"));
        p.setDescricao(request.getParameter("descricao"));
        p.setCodBarras(Integer.parseInt(request.getParameter("codBarras")));
        p.setCategoria(request.getParameter("categoria"));
        p.setValor(Double.parseDouble(request.getParameter("valor")));
        p.setQuantidade(Integer.parseInt(request.getParameter("quantidade")));
        
        dao.cadastrar(p);
        
        RequestDispatcher rd = request.getRequestDispatcher("/lista.jsp");
        rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}

2 Respostas

ErickRAR

O servlet está mapeado no web.xml?

B

Seria adicionar esse código no web.xml

<servlet> <servlet-name>CadastroProduto</servlet-name> <servlet-class>br.com.estoque.CadastroProduto</servlet-class> </servlet> <servlet-mapping> <servlet-name>CadastroProduto</servlet-name> <url-pattern>/CadastroProduto</url-pattern> </servlet-mapping>

Criado 5 de maio de 2014
Ultima resposta 6 de mai. de 2014
Respostas 2
Participantes 2