0

I want to disconnect from a jsp page, in order to do that, here's what I tried :

In my JSP (accueil_mobile.jsp) I got this :

<form action="b" method="POST">
      <input type="submit" value="Deconnexion" />
</form>

b refers to a SERVLET of which post's method is as follows :

public static final String VUE = "/accueil_mobile.jsp" ;
.
.
.
protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
       request.getSession().invalidate();
       response.sendRedirect("accueil.xhtml");
       this.getServletContext().getRequestDispatcher(VUE).forward(request, response) ;
     }

Now I expected this to invalidate the session and redirect me to accueil.xhtml, but all it does is load indifinitly the page. Why is that ?

Thanks.

Akheloes
  • 1,352
  • 3
  • 11
  • 28

2 Answers2

0

Try to remove

this.getServletContext().getRequestDispatcher(VUE).forward(request, response) ;

You are trying to use redirect and forward at the same time.

Alex
  • 11,451
  • 6
  • 37
  • 52
  • Removed it, the behavior remains. – Akheloes Jun 18 '13 at 23:57
  • You should use full path for redirection. I think you don't have access directly to accueil.xhtml. Try to redirect to servlet related to this page, for example. – Alex Jun 19 '13 at 00:02
  • The two pages are in the same folder. Plus, to `.xhtml` to which I redirect has no servlet, it's a JSF page. – Akheloes Jun 19 '13 at 00:06
0

You need to change for the view and not the external URL, get rid of the sendRedirect :

public static final String VUE = "/accueil_mobile.xhtml" ;

protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    request.getSession().invalidate();
    //response.sendRedirect("accueil.xhtml");
    request.getRequestDispatcher(VUE).forward(request, response) ;
}
Alexandre Lavoie
  • 8,711
  • 3
  • 31
  • 72
  • You mean like this `this.getServletContext().getRequestDispatcher("/accueil.xhtml").forward(request, response) ;` ? If so, it doesn't work either. One question : is it necessary that the page to which I'll redirect possess a servlet of its own ? I have a feeling that the problem is the I am trying to pass from a jsp page to an .xhtml page. – Akheloes Jun 19 '13 at 07:44
  • Apparently that's no problem as this thread explain it's doable [Here](http://stackoverflow.com/questions/11223556/can-we-dispatch-request-to-an-html-inside-servlet) – Akheloes Jun 19 '13 at 07:47
  • Well if you are redirecting to a JSF page, you need the view name (`accueil.xhtml` or `accueil_mobile.xhtml` ...) from a servlet there is no problem doing that. I'm just not sure if you really are configured in JSF or JSP. – Alexandre Lavoie Jun 19 '13 at 10:22
  • _"Configured in JSF or JSP"_, didn't get this one. – Akheloes Jun 19 '13 at 10:34
  • Your real files are ending with `.xhtml` or `.jsp`? Do you have a `Faces Servlet` servlet mapping in `web.xml`? – Alexandre Lavoie Jun 19 '13 at 10:42
  • Oh that. Well, my project is basically a JSF one, but I needed few JSP page, so added them. – Akheloes Jun 19 '13 at 10:43
  • I've not done JSP since a while, so probably the `VUE` should be `.jsp` if it is so in your file system, ortherwise, `.xhtml` (depending if it is a JSP or JSF page). – Alexandre Lavoie Jun 19 '13 at 10:45
  • Expected that, maybe to redirect from a servlet : the page to which we redirect must be jsp and have a servlet of its own. Thanks though. – Akheloes Jun 19 '13 at 10:49