3

A la hora de declarar un form html en un jsp. el programa corre pero la clase me dice " Element 'UserHandler' not found ", porque me aparece este error? estos son mis archivos user.jsp

<%@ 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=ISO-8859-1">
<link href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/css/materialize.min.css" rel="stylesheet"/>
<link type="text/css" rel="stylesheet" href="resources/css/miPrimerCSS.css" />

<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.98.2/js/materialize.min.js"></script>
<title>Add New User</title>
</head>


<body>
    <form method="POST" action='UserHandler' name="frmAgregarUsuario">
    <input type="hidden" name="action" value="insert" />



    <div class="card-panel teal lighten-2">
      <span class="white-text text-darken-2">AGREGAR UN NUEVO RECORD</span>
    </div>

<table>






    <tr>
        <td><input type="text" name="userid" placeholder=" ID DE USUARIO " /></td>
    </tr>

    <tr>
        <td><input type="text"  name="firstName" placeholder=" INGRESE EL NOMBRE " /></td>
    </tr>
    <tr>
        <td><input type="text"  name="lastName" placeholder=" INGRESE EL APELLIDO " /></td>
    </tr>
    <tr>


        <td><button class="btn waves-effect waves-light" type="submit" name="action">Enviar</button></td>

    </tr>
</table>
</form>

<!--   -->
<p><a class="waves-effect waves-light btn-large " href="UserHandler?action=listUser">Todos los Records</a>


</body>
</html>

UserHandler.java

package net.roseindia.handler;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.roseindia.dao.UserDao;
import net.roseindia.bean.UserBean;

public class UserHandler extends HttpServlet {    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private static String INSERT = "/user.jsp";
    private static String Edit = "/edit.jsp";
    private static String UserRecord = "/listUser.jsp";
    private UserDao dao;

    public UserHandler() {
        super();
        dao = new UserDao();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String redirect="";
        String uId = request.getParameter("userid");        
        String action = request.getParameter("action");
        if(!((uId)== null) && action.equalsIgnoreCase("insert"))
        {
            int id = Integer.parseInt(uId);
            UserBean user = new UserBean();
            user.setId(id);
            user.setfName(request.getParameter("firstName"));
            user.setlName(request.getParameter("lastName"));
            dao.addUser(user);
            redirect = UserRecord;
            request.setAttribute("users", dao.getAllUsers());    
            System.out.println("Record Added Successfully");
        }
        else if (action.equalsIgnoreCase("delete")){
            String userId = request.getParameter("userId");
            int uid = Integer.parseInt(userId);
            dao.removeUser(uid);
            redirect = UserRecord;
            request.setAttribute("users", dao.getAllUsers());
            System.out.println("Record Deleted Successfully");
        }else if (action.equalsIgnoreCase("editform")){         
            redirect = Edit;            
        } else if (action.equalsIgnoreCase("edit")){
            String userId = request.getParameter("userId");
            int uid = Integer.parseInt(userId);            
            UserBean user = new UserBean();
            user.setId(uid);
            user.setfName(request.getParameter("firstName"));
            user.setlName(request.getParameter("lastName"));
            dao.editUser(user);
            request.setAttribute("user", user);
            redirect = UserRecord;
            System.out.println("Record updated Successfully");
         } else if (action.equalsIgnoreCase("listUser")){
            redirect = UserRecord;
            request.setAttribute("users", dao.getAllUsers());
         } else {
            redirect = INSERT;
        }

        RequestDispatcher rd = request.getRequestDispatcher(redirect);
        rd.forward(request, response);
    }

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

introducir la descripción de la imagen aquí

1
  • ¿El error te sale solo en el IDE? Porque parece un error del validador; yo intentaría ejecutarlo a ver qué pasa.
    – SJuan76
    Commented el 23 jun. 2017 a las 7:50

1 respuesta 1

1

Me parece que te falta declarar el servlet en el web.xml:

<servlet>
    <servlet-name>MiUserHandler</servlet-name>
    <servlet-class>mi.paquete.com.UserHandler</servlet-class>
  </servlet>
<servlet-mapping>
    <servlet-name>MiUserHandler</servlet-name>
    <url-pattern>/UserHandler</url-pattern>
 </servlet-mapping>

Y en el jsp invocar el action con diagonal action='/UserHandler'

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.