How to call a java method from a button in jsp, help please!

0

Hello good afternoon friends, I hope and they are well, previously I had already asked in another occasion and because here I will write it again not to cause confusion, I have this java code that later I put it in a method that I did

 public void eliminarProcesos(); {
        String fechaMaximaFinal=null;
    String fechaMaximaInicial=null;
        String whereVAAC;
    String whereVAACOpera;

    try {
        if(fechaMaximaInicial.startsWith("00")){
            whereVAAC=" to_date(NB_FECHA,'DDMMYYYY') between to_date('01"+fechaMaximaInicial.substring(2)+"','DD/MM/YYYY') and to_date('"+fechaMaximaFinal+"', 'DD/MM/YYYY') ";
            whereVAACOpera=" to_date(NB_FECHA,'DDMMYYYY') between to_date('01"+fechaMaximaInicial.substring(2)+"','DD/MM/YYYY') and to_date('"+fechaMaximaFinal+"', 'DD/MM/YYYY') ";
        }else{
            whereVAAC=" to_date(NB_FECHA,'DDMMYYYY') between to_date('"+fechaMaximaInicial+"','DD/MM/YYYY') and to_date('"+fechaMaximaFinal+"', 'DD/MM/YYYY')";
            whereVAACOpera=" to_date(NB_FECHA,'DDMMYYYY') between to_date('"+fechaMaximaInicial+"','DD/MM/YYYY') and to_date('"+fechaMaximaFinal+"', 'DD/MM/YYYY')";
        }
        //Aqui es donde borra los registros de las tablas 54 y 49
        this.genericDao.borrarRegistros("VivVAACOpera", whereVAACOpera);
        this.genericDao.borrarRegistros("VivVAAC", whereVAAC);

    }catch (Exception e){
        logger.info("exception: " + e.getMessage());
    }

    }

that invokes a hibernate and that hibernate generates the delete statement, my main problem is as follows, how could you invoke that java method using a button in a jsp? this the code of my button I want, but I do not have an idea how to do it:

 <input id="#" name="#" type="button" value="Borrar Proceso Completo"   onclick="javascript:borrarAllProcesoVAAC();"/> 

and as such this would be my javascript function

function borrarAllProcesoVAAC(){
            document.getElementById.action="eliminarAllProcesoVaac"
                   document.getElementById.submit();

        }

I have also contemplated doing it with a servlet but I am not if it is necessary, it is the following:

public void eliminarAllProcesoVaac(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{


    }

I hope and you can help me with this problem, I remain on the side if it is not understood.

Best regards.

    
asked by JUAN JOSE BUSTAMANTE SOLIS 30.05.2018 в 19:33
source

1 answer

0

To call a server-side method, you must create a form that calls the servlet and it in turn executes that method. An example for your case would be like this:

Customer side:

<form action="MiServlet" method="post">
 <input type="submit" value="Borrar Proceso Completo"/>
</form>

Server Side (Servlet):

public class MiServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

             eliminarProcesos();
        } finally {
            out.close();
        }
    }

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

Description of the process: The html form points to the Servlet, which calls the method " processRequest " that executes the desired logic. You must build the respective response or redirection to the desired resource. The above could be done through AJAX technology but it is already at your disposal.

    
answered by 08.01.2019 в 23:55