0

My web app directory structure is

myApp
 -src
     - filtersPackage(all my filters lie in this package)
     - servletsPackage(all my controllers and models lie in this package)
 - web-content           
   - web
     - views(all my jsp files lie in this dir)
     - js(all my javascript files lie in this dir)

In login.jsp, user clicks on FB login button, inside js/FBAUth.js I collect login details and I send it to my auth.do servlet using jquery ajax POST method.

The SessionFilter allows the request to go to AuthServlet's doPost method. In AuthServlet If the credentials are correct then do the following

url = "dashboard.jsp" 
request.getSession().setAttribute("uid", id);               
view = request.getRequestDispatcher(url);
view.forward(request, response);
return;

I have seen in debug mode that these lines execute but my browser is still stuck on login page. The values for view.requestURI -> /myApp/web/views/dashboard.jsp and view.servletPath -> /web/views/dashboardTmp.jsp. I also tried response.sendRedirect(url), but still the browser is stuck on login page. Nothing executes after these lines.

In web.xml, my auth servlet is mapped as follows

<servlet>
    <servlet-name>Auth</servlet-name>
    <servlet-class>servletsPackage.AuthServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Auth</servlet-name>
    <url-pattern>/web/views/auth.do</url-pattern>
</servlet-mapping>

I also tried calling doGet inside doPost, and executed this code in doGet, but all in vain. What am I missing here ?

Sai Ye Yan Naing Aye
  • 6,622
  • 12
  • 47
  • 65
Timmy Simons
  • 599
  • 8
  • 21

2 Answers2

1

This approach seems to be a bit flawed. Your forward is not working because you are using AJAX to post data. You would need to use javascript or rather jquery to handle the redirection.

Check if your jquery AJAX function's callback method is working. print out the responseText using alert(responseText);

Check this link for more information about your problem. And check the first answer. It provides a solution for this problem.

Community
  • 1
  • 1
Sreenath S
  • 1,269
  • 1
  • 14
  • 21
  • Thnx a ton for the answer. I would like to change my design so as to make the servlet forwards the jsp. I just want to save the roundtrip caused by this callback approach. Is there any other way ? – Timmy Simons Aug 31 '12 at 13:27
  • This is not a complex approach at all. I hope you are not going to do page navigation like this for every page, are you ? This is just for login purposes, right ? By the way, using this approach you can send the redirection url as response, ie you can change login home page dynamically for this webapp. So this approach has a hidden advantage as well. – Sreenath S Aug 31 '12 at 14:30
  • suppose that on logging in and using the app for some time, for some intentional reason the session is invalidated, now if user clicks on any jquery load button, i want the server to redirect to login instead of sending url in response. i hav this problem because there are many jquery ajax and jquery load calls in a page that ill have to modify using your approach. – Timmy Simons Oct 07 '12 at 05:28
  • well its not particularly my approach, almost all the AJAX webapps use this approach. and you are not the first one to have this dilemma either. chk this out: http://stackoverflow.com/questions/2927044/redirect-on-ajax-jquery-call or this http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call. tell me if this doesn't solve your problem. – Sreenath S Oct 07 '12 at 07:59
0

Try to return something from your servlet (write in response writer), retrieve that as ajax response, depending upon that, do a window.location.href ="YOUR URL";

Ankit
  • 3,083
  • 7
  • 35
  • 59