Foros del Web » Programación para mayores de 30 ;) » Java »

Problema con servlet

Estas en el tema de Problema con servlet en el foro de Java en Foros del Web. Buenos días a todos os comento: Estoy tratando de hacer un formulario dentro de un JSP para recoger los datos de un usuario para que ...
  #1 (permalink)  
Antiguo 04/02/2009, 05:11
 
Fecha de Ingreso: diciembre-2007
Mensajes: 36
Antigüedad: 16 años, 4 meses
Puntos: 0
Exclamación Problema con servlet

Buenos días a todos os comento:

Estoy tratando de hacer un formulario dentro de un JSP para recoger los datos de un usuario para que se de de alta donde tengo un bean

Código:
<jsp:useBean
    id="BeanCliente" scope="request" class="patrones.modelo.cliente.BeanAltaCliente" />
    <jsp:setProperty name="BeanCliente" property="*"/>
Os pongo un ejemplo de como capturo los valores

Código:
 <td> <input name="nombre" value="<jsp:getProperty name="BeanCliente" property="nombre"/>"
                       type="text" size="30" maxlength="20" /> </td>
Y aquí os pongo el código desde el servler

Código:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package patrones.modelo.cliente;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import patrones.modelo.fachadacliente.FachadaCliente;
import patrones.modelo.cliente.BeanAltaCliente;
import patrones.util.modelo.excepciones.ErrorInternoException;

/**
 *
 * @author Administrador
 */
public class ServletProcesoAltaCliente 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
     */
    public String nombre;
    public String apellido;
    public String cuenta;
    public String telefono;
    public String email;
    public String password;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        /*response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();*/
        try {
            /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet ServletProcesoAltaCliente</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet ServletProcesoAltaCliente at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
             */

            BeanAltaCliente miCliente = (BeanAltaCliente) request.getAttribute("BeanCliente");
            if (miCliente == null) {
                response.sendRedirect("/TrabajoObligatorio/Error.jsp");
            } else {
                nombre=miCliente.getNombre();
                apellido=miCliente.getApellido();
                cuenta=miCliente.getCuenta();
                telefono=miCliente.getTelefono();
                email=miCliente.getEmail();
                password=miCliente.getPassword();
                FachadaCliente.crearCliente(nombre,apellido,telefono,cuenta,email,password);
                response.sendRedirect("/TrabajoObligatorio/ExitoAlta.jsp");
            }

        } catch (ErrorInternoException e) {
            response.sendRedirect("/TrabajoObligatorio/Error.jsp");
        } finally {
            //out.close();
        }
    }

    // <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>
}
El caso es que al ejecutar la aplicación cuando hace la llamada desde el jsp al servlet me da error el siguiente error:

type Exception report

message

descriptionThe server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException

note The full stack traces of the exception and its root causes are available in the GlassFish/v3 logs.

La deducción es que o no llama correctamente al bean o que lo devulv vacio, alguna idea?

Última edición por Ciberyo; 04/02/2009 a las 05:20
  #2 (permalink)  
Antiguo 05/02/2009, 05:31
 
Fecha de Ingreso: diciembre-2007
Mensajes: 36
Antigüedad: 16 años, 4 meses
Puntos: 0
Respuesta: Problema con servlet

Voy a tratar de aportar más información a ver si así alguien descubre el fallo, para la base de datos instale el appserv y he creado la base de datos con el phpmyadmin

os pongo la clase donde esta la información para la conexión al a base de datos

Código:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class GestorConexiones {
    private final static String DRIVER = "com.mysql.jdbc.Driver";
    private final static String URL = "jdbc:mysql://localhost/tiendajava";
    private final static String USUARIO = "root";
    private final static String CLAVE = "root";
    /*
    private static final Context contexto;
    private static final DataSource ds;
    */
    static {

        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace(System.err);
        }
        /*try {
            contexto = new InitialContext();
            ds = (DataSource) contexto.lookup(URL);
        } catch (NamingException ex) {
            throw new RuntimeException(ex);
        }*/
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USUARIO, CLAVE);
        //return ds.getConnection(USUARIO, CLAVE);
    }
}
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 18:49.