request.getRequestDispatcher()失败

时间:2016-02-02 16:24:32

标签: java servlets

我有一个功能没有达到他的目的。我想在catch子句中重定向,但他没有这样做。我不得不说我的课程不能扩展到HttpServlet,因为他会扩展到另一个类。他没有重定向到jsp,为什么?他只是在忽视它。谢谢你的阅读。

这是代码:

 private Integer gestionEnvios(HttpServletRequest request,MantenimientoEnvio buscador) throws ProcedureException {

    int tipoD = 0;
    Integer resul = null;       
    String formulario=buscador.getBuscadorFormularios();

    if(formulario.equals("ACTUALIZAR ESTADO"))
    {   
        String referenciaE=buscador.getBuscadorReferenciaE();
        String comprobacionTextarea=contarCaracteres(referenciaE);
        String estado=buscador.getBuscadorEstado(); 
        int codigo=devolvercodigo(estado);      
        String fechaDesde=buscador.getBuscadorFechaDesde();
        String fechacorrecion=new String();
        try {

         fechacorrecion=comprobaciónFecha(fechaDesde); 

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            System.out.println("Has introducido mal la fecha");     
            e.printStackTrace();
//              request.getRequestDispatcher("/clock.jsp");
        }

/***************************/ This is the full code of the function, he doesnt end when reaches request.getRequestDispatcher("/clock.jsp").forward(request, response); hes continues...

/*****/ private Integer gestionEnvios(HttpServletRequest request,MantenimientoEnvio buscador, HttpServletResponse response) throws ProcedureException, ServletException, IOException {

    int tipoD = 0;
    Integer resul = null;       
    String formulario=buscador.getBuscadorFormularios();

    if(formulario.equals("ACTUALIZAR ESTADO"))
    {   
        String referenciaE=buscador.getBuscadorReferenciaE();
        String comprobacionTextarea=contarCaracteres(referenciaE);
        String estado=buscador.getBuscadorEstado(); 
        int codigo=devolvercodigo(estado);      
        String fechaDesde=buscador.getBuscadorFechaDesde();
        String fechacorrecion=new String();
        try {

         fechacorrecion=comprobaciónFecha(fechaDesde); 

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            System.out.println("Has introducido mal la fecha");     
//              e.printStackTrace();
            request.getRequestDispatcher("/clock.jsp").forward(request, response);
        }


        if(referenciaE.contains(",")){
            String prepara=referenciaE.trim();
            String prepara0=prepara.replaceAll("\\s+", " ");
            String prepara1=prepara0.replaceAll(",( )*"," ");
            String prepara2=prepara1.replaceAll(" ", ",");
            if(fechaDesde.equals("")){  
                resul=mantEnvioService.gestionEnvio(2, codigo,null, prepara2);
            }else{
                resul=mantEnvioService.gestionEnvio(2, codigo, fechaDesde, prepara2);
            }

    }else
    {   
        if(fechaDesde.equals("")){  
            resul=mantEnvioService.gestionEnvio(2, codigo,null, referenciaE);
        }else{
            resul=mantEnvioService.gestionEnvio(2, codigo, fechaDesde, referenciaE);
        }
     }

    }
    else if(formulario.equals("DEVOLUCIÓN")){
        String referenciaD=buscador.getBuscadorReferenciaD();
        String cadena=new String();
        /**
         * Carlos Gil
         * Eliminamos el retorno de carro por un punto 
         * 
         */
        cadena=eliminarRetornoCarro(referenciaD);
        String semanl=buscador.getBuscadorEjecucionSemanal();
        String tipo=buscador.getBuscadorButtons();
        if(tipo.equals("O2O")&&semanl==null){
            tipoD=1;
        }else if(tipo.equals("ICP")){
            tipoD=2;
        }else if(tipo.equals("Otros")){
            tipoD=3;
        }else if(tipo.equals("O2O Ejecucion Semanal")){
            tipoD=4;    
        }
        if(referenciaD.contains(",")){
            String prepara=referenciaD.trim();
            String prepara0=prepara.replaceAll("\\s+", " ");
            String prepara1=prepara0.replaceAll(",( )*"," ");
            String prepara2=prepara1.replaceAll(" ", ",");
        resul=mantEnvioService.gestionEnvio(1, tipoD, null, prepara2);
        }else   
        resul=mantEnvioService.gestionEnvio(1, tipoD, null, referenciaD);
    }   

    return resul;
}

2 个答案:

答案 0 :(得分:0)

假设您在方法中添加了HTTPServletResponse 响应参数,请致电:

request.getRequestDispatcher("/clock.jsp").forward(request, response);

答案 1 :(得分:0)

Java project structural best practices

 basic-maven-project/ 

 |-- pom.xml
 |-- src
 |   |-- main
 |   |   |-- java
 |   |   |-- resources
 |   |   `-- webapp

 |   |       `-- WEB-INF
     |   `-- test

|       |-- java

|       `-- resources
        `-- target
|-- classes
       `-- test-classes

您可以选择转发和重定向到其他页面。

- request.getRequestDispatcher("/clock.jsp").forward(request, response);

- response.sendRedirect("clock.jsp");

话虽如此,如果您希望将您的网页保留在WEB-INF下,请参阅此问题:How to properly put JSPs in the WEB-INF folder?,该主题会被多个用户触及。接受的答案来自Teja

更明确的推理:Why the JSP pages under WEB-INF can't be accessible with sendRedirect method?