0

I'm working on a Java Web app, in which I have a JSP with a form. Inside the form is a table, showing information, and for each row there's a "Modify" button. When pressed, I have a Javascript script replace the labels in the table with inputs for the form, which the user can fill out and send a request by clicking the "Accept" button.

However, when using request.getParameter() in the servlet, it returns None, as if the inputs didn't exist. The inputs are indeed created, the post is performed, and the adequate method in the servlet is called, but when I try to access the parameters, they are Null.

Here's the code:

listadoCatLibro.jsp:

<form id="form-mod" action="/libreria-java/admin/listado-cat-libro/modificar" method="post" enctype="text/plain">
    <table class="table">
      <thead>
        <tr>
          <th scope="col">ID</th>
          <th scope="col">Descripcion</th>
          <th scope="col">Estado</th>
          <th scope="col"> </th>
          <th scope="col"> </th>
        </tr>
      </thead>
      
      <tbody>
        <c:if test="${requestScope.categorias != null }">
          <c:forEach var="cat" begin="0" items="${requestScope.categorias}">
              <tr id="row-cat-${cat.getId()}">
                  <th id="id-lbl-${cat.getId()}" scope="row">${cat.getId()}</th>
                  <td id="des-lbl-${cat.getId()}">${cat.getDesc()}</td>
                  <td id="est-lbl-${cat.getId()}">${cat.getEstado()}</td>
                  <td> <button id="mod-btn-${cat.getId()}" 
                               class="btn btn-secondary float-right"
                               type="button"
                               onClick="modificar(${cat.getId()})">Modificar</button></td>
                               
                  <td> <button id="del-btn-${cat.getId()}" 
                               class="btn btn-secondary float-right"
                               type="button"
                               onClick="eliminar(${cat.getId()})">Eliminar</button></td>
              </tr>
            </c:forEach>    
        </c:if> 
    
      </tbody>
    </table>
</form>

categorias.js

function modificar(id){
if (modificando == false){
            
    modificando = true;
    var sid = id.toString();
    oldRow = document.getElementById('row-cat'+sid);

    idlbl = document.getElementById("id-lbl-"+sid);
    idTD = document.createElement('td');
    newID = document.createElement('input');
    newID.type="text";
    newID.value=idlbl.innerHTML;
    newID.name="inputID";
    newID.style.width="50px";
    newID.setAttribute("readonly","true");
    newID.className="form-control";
    idTD.appendChild(newID);
    idlbl.parentNode.replaceChild(idTD, idlbl);
    
    desclbl = document.getElementById("des-lbl-"+sid);
    descTD = document.createElement('td');
    newDesc = document.createElement('input');
    newDesc.type="text";
    newDesc.value=desclbl.innerHTML;
    newDesc.name="inputDesc";
    newDesc.className="form-control";
    descTD.appendChild(newDesc);
    desclbl.parentNode.replaceChild(descTD, desclbl);
    
    estlbl = document.getElementById("est-lbl-"+sid);
    estTD = document.createElement('td');
    newEst = document.createElement('input');
    newEst.type="text";
    newEst.value=estlbl.innerHTML;
    newEst.name="inputEstado";
    newEst.className="form-control";
    estTD.appendChild(newEst);
    estlbl.parentNode.replaceChild(estTD, estlbl);
    
    modbtn = document.getElementById("mod-btn-"+sid);
    newMod = document.createElement('button');
    newMod.className="btn btn-primary";
    newMod.innerHTML="Aceptar";
    modbtn.parentNode.replaceChild(newMod, modbtn);
    
    delbtn = document.getElementById("del-btn-"+sid);
    newDel = document.createElement('button');
    newDel.className="btn btn-secondary";
    newDel.innerHTML="Cancelar";
    newDel.setAttribute('type', 'button');
    delbtn.parentNode.replaceChild(newDel, delbtn);
}
}

And ServletAdmin.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
    try {
        
        if(isAdmin(request)) {
            switch (request.getPathInfo()) {        
            case "/alta-libro":
                this.altaLibro(request, response);
                response.sendRedirect("/libreria-java/admin/alta-libro");
                break;  
            case "/listado-cat-libro/alta":
                this.altaCatLibro(request, response);
                response.sendRedirect("/libreria-java/admin/listado-cat-libro");
                break;  
            case "/listado-cat-libro/modificar":
                this.modificarCatLibro(request,response);
                response.sendRedirect("/libreria-java/admin/listado-cat-libro");
                break;
            }
            
        } else {
            response.sendRedirect("/libreria-java/home");
        }   
        
        
    } catch (CustomException e) {
        request.getSession().setAttribute("errorMsg", e.getMessage());
        request.getRequestDispatcher( "/WEB-INF/pages/error.jsp" ).forward( request, response );
    } catch (Exception ex) {
        CustomException e = new CustomException("Error desconocido", "ServletAdmin", ex);
        request.getSession().setAttribute("errorMsg", e.getMessage());
        request.getRequestDispatcher( "/WEB-INF/pages/error.jsp" ).forward( request, response );
    }
}

    private void modificarCatLibro(HttpServletRequest req, HttpServletResponse res) {
    CtrlCategoria ctrl = new CtrlCategoria();
    Categoria c = new Categoria();
    System.out.println(req.getParameter("inputID"));
    System.out.println(req.getParameter("inputDesc"));
    System.out.println(req.getParameter("inputEstado"));
    c.setId(Integer.parseInt(req.getParameter("inputID")));
    c.setDesc(req.getParameter("inputDesc"));
    c.setEstado(req.getParameter("inputEstado"));
    ctrl.update(c);
    
}
}

Thank you for any help you can provide, and sorry for the long and messy code. I didn't want to edit out much in case I accidentally omitted whatever is causing the issue.

1 Answers1

0

EDIT- I found the problem. The issue was with the declaration of the form in the JSP file. This line:

<form id="form-mod" action="/libreria-java/admin/listado-cat-libro/modificar" method="post" enctype="text/plain">

should have been:

<form id="form-mod" action="/libreria-java/admin/listado-cat-libro/modificar" method="post">

the "enctype="text/plain"" was causing the problem.