4 Replies Latest reply on Oct 5, 2009 3:36 PM by Jaime Martin

    redirecting from xhtml to jsp

    Jaime Martin Apprentice

      Hi!
      I haven´t used jsp for some years, howewer from a seam project I must redirect to some old jsp pages.
      I´m trying it this way


      public static void redirect() {
           Redirect.instance().setViewId("/jmp.jsp");
           Redirect.instance().execute();
       }   
      



      but with that it is looking for jmp.xhtml instead of jmp.jsp (I´ve created a jmp.xhtml and checked it).
      Is there a configuration issue or something to be taken into account so as to redirect to a jsp page from a xhtml one?
      thanks in advance!

        • 1. Re: redirecting from xhtml to jsp
          Nikos Paraskevopoulos Novice

          Hi,


          Indeed the Javadocs for Redirect.setViewId() specify Set the JSF view id to redirect to. This is a JSF redirection, so it correctly translates the suffix.


          You need to get the response object from the ExternalContext, then call the sendRedirect() method. The response object is either HttpServletResponse for a servlet environment, or the corresponding class for a portlet environment, so you need to take care. After calling sendRedirect(), notify the JSF engine by calling FacesContext.responseComplete().

          • 2. Re: redirecting from xhtml to jsp
            Jaime Martin Apprentice

            thank you. I tried it this way but it doesn´t work. No error displayed, no excepction catched but it doesn´t redirect. am I missing anything?



                 try {
                     
                 FacesContext context = FacesContext.getCurrentInstance();
                 HttpServletResponse respon  = ((HttpServletResponse)context.getExternalContext().getResponse());
                 respon.sendRedirect("C:/ECLIPSE321/workspace/TAIE/view/jmp.jsp");
                 context.responseComplete();
                 }
                 catch (Exception ex) {
                     ex.printStackTrace();
                 }
            



            btw, I use full path because I have an error if I just had


            respon.sendRedirect("/jmp.jsp");
            



            thank you!


            • 3. Re: redirecting from xhtml to jsp
              Nikos Paraskevopoulos Novice
                   respon.sendRedirect("C:/ECLIPSE321/workspace/TAIE/view/jmp.jsp");
              




              Ouch! This is totally wrong!


              The sendRedirect() commands the browser to load another URL. From the Javadocs: If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root..


              So if your JSF view is found at URL http://my.host.com/ctx/a/b/c.xhtml and you need to redirect to http://my.host.com/ctx/foo/bar.jsp try:


              response.sendRedirect(request.getContextPath() + "/foo/bar.jsp");
              



              You need to get the request object from the FacesContext, in a way similar to the response object.


              This may still not work, if the current request is a RichFaces AJAX request. (Or it may work, I haven't tried it.) If it doesn't work indeed, we will figure out another way. But try this first.


              Good luck!

              • 4. Re: redirecting from xhtml to jsp
                Jaime Martin Apprentice

                thank you, it works that way!