简体   繁体   中英

jsf ajax redirect from servlet filter not working

I've a servlet filter that will perform a redirect when the session has expired.

For non-ajax request, the filter execute the HttpServletResponse.sendRedirect(myUrl) to perform the redirect, which works great. But, I can't say the same with ajax type request.

For ajax request, the filter execute the HttpServletResponse.getWriter().println(partialResponseContent) to perform the redirect. It doesn't work. The screen stay the same, seems to be freeze and with all input fields inhibited. Any pointer on what I might be missing or what I can try to figure out the cause of problem?

Below is the partialResponseContent:

<?xml version="1.0" encoding="UTF-8"?><partial-response><redirect url="/MyAccount/command?cmd=twoFA&TargetUrl=/MyAccount/viewDevices.jsf"></redirect></partial-response>

I double checked it in Chrome's Developer Tools screen and sees that the content is being sent correctly (See attached image).

在此处输入图片说明

Wasted two days for this. But I'm glad to finally get it to work. The problem was caused by the value of the url attribute (in the redirect element) was not xml encoded. My url contains a & character. It needs to be encoded to & amp; for the redirect to work. Note: I used the org.apache.commons.lang.StringEscapeUtils.escapeXml to do the xml encoding.

Below is the before encoding:

<?xml version="1.0" encoding="UTF-8"?><partial-response><redirect url="/MyAccount/command?cmd=twoFA&TargetUrl=/MyAccount/viewDevices.jsf"></redirect></partial-response>

Below is the after encoding:

<?xml version="1.0" encoding="UTF-8"?><partial-response><redirect url="command?cmd=twoFA&amp;TargetUrl=/MyAccount/viewDevices.jsf"></redirect></partial-response>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM