STRUTS2 Interceptors intercept AJAX requests

Source: Internet
Author: User

Interceptor Code:
@Override
Public String intercept (actioninvocation invocation) throws Exception {

Actioncontext CTX = Invocation.getinvocationcontext ();
Map session = Ctx.getsession ();
User user = (user) session.get (constants.session_user_key);
if (user = null) {
HttpServletRequest request = Servletactioncontext.getrequest ();
HttpServletResponse response = Servletactioncontext.getresponse ();
PrintWriter pw = Response.getwriter ();

String flag = "";

Interception of Ajax requests if (Request.getheader ("X-requested-with")!= null
&& Request.getheader ("X-requested-with"). Equalsignorecase (
"XMLHttpRequest")) {
Log.info ("User does not log in or logon expires, cannot access");
Response.setcharacterencoding ("Text/html;charset=utf-8");
Response.setcontenttype ("Text/html;charset=utf-8");
Flag = "9999";
Pw.write (flag);

return null;

is not an asynchronous requested intercept} else {
Response.setcharacterencoding ("Text/html;charset=utf-8");
Response.sendredirect ("/businesstest/login.jsp");
Log.info ("User does not log in or logon expires, cannot access");
return "Login";
}
}
return Invocation.invoke ();

}


JSP page code

//Login Authentication  
Ext.Ajax.on (' Requestcomplete ', checkusersessionstatus, this);  //All asynchronous requests will take the event
function Checkusersessionstatus (conn,response,options) {
var str = response.responsetext;
if (str = = ' 9999 ') {
alert (' The connection has timed out, please login again! ');
Window.location.href = ' login.jsp ';
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.