Atticus
Atticus

Reputation: 1632

Servlet redirect doesn't display requested page

I have a page - lets call it page X - where I do an ajax call that populates my table constructed with jqGrid. So here I call localhost:8080/servlet/ajax/Data.json which is a servlet. Now, if the page expires and the user still wants to access the table (search data for instance) I would like to redirect the request to the Login page.

The ajax call calls the Data.json servlet that is not in the same servletcontext as my Login page (which is localhost:8080/servlet/myapp), therefore I cannot use servlet forward (I am using JBoss 4.2 and I can only forward in the same servletcontext).

I have tried to redirect with sendRedirect, which is able to call the Login page that is actually executed but not displayed. And this is where I got lost. My Login page is executed and I still see page X. I have tried to write the returned HTML (from Login page) into the response of page X (I also set the Content-Type to text/html). The HTML is returned in the response, but not displayed.

I am using Apache Turbine which is another servlet that retrieves a page when requested. Do you have any ideas? I don't know what to do.

Upvotes: 0

Views: 843

Answers (2)

kosa
kosa

Reputation: 66657

Ajax calls are asynchronous calls, sendRedirect call on ajax calls never be effective (ajax response always sends response to initiator).

You may need to use XMLHttpRequest status code to find out the page expiration and then use java script window.location to reset the browser url to login url.

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143946

It looks like your servlet at servlet/ajax/Data.json is telling the AJAX request to redirect to the login page. Then the page is going to try to render the jqGrid table with the login page, which probably won't work.

You need to add code where you are making the AJAX call to servlet/ajax/Data.json to check to see whether the browser itself needs to be redirected. Either if the servlet returns a 302 redirect as a response, or if the servlet returns an error that's simply "SESSION EXPIRED", so the javascript on the browser's end that's making the AJAX call to the servlet knows the session has expired and to redirect the browser to the login page.

Upvotes: 2

Related Questions