Recently in the company to do the project has been testing and use are very good, customer response is very good, but, in the first two days, the customer put forward a bug, but the whole team are frown, in today's bus, I suddenly think of a possibility, is that we are accustomed to the Ajax submitted a problem!
causes:
requirements: In determining the user session has no time-out situation, we use in all the request through AOP to judge, if the session, the user login information is lost, then re-login, otherwise continue!
Bug situation: In the page submission, jump under normal judgment, but after the user session expires, the direct use of Ajax submission, the submission failed!
Initial Program:
Scenario 1:
Modify session Expiration time (no root causes)
Scenario 2:
Abstract public JS, first verify that the session is logged in, not logged in, then return to the landing page (personally think the best, because in the original based on not change, increase, not modify)
Scenario 3:
Abstract public AJAX scheme, using the Ajax method, unified Call this method
Pros and Cons Analysis:
Modify the session failure time (not a cure) palliative, but also cause system performance degradation, should be debugging to a suitable time, or with a cookie scheme instead!
Abstract public JS, abstract public AJAX scheme, each call needs to change, too big changes, resulting in system maintenance work increased!
More Elegant solutions:first, an interceptor was built to determine the session timeout. User Login will save user information in a session, in the session of the listener, session timeout will destroy the user information stored in the session, and the interceptor through the session whether there is user information to determine the session timeout. (I always think this method is not good.) I don't know what better way to do it. )
Interceptors are SPRING-MVC interceptors that determine whether an AJAX request is in the Interceptor:
<span style= "FONT-SIZE:18PX;" >1.public boolean prehandle (HttpServletRequest request, 2. HttpServletResponse response, Object handler) throws Exception 3. { 4. if (Request.getsession (). getattribute ("user") = = NULL)//Determine if there is user information in session 5. { 6. if (Request.getheader ("X-requested-with")! = null 7. && Request.getheader ("X-requested-with") 8. . Equalsignorecase ("XMLHttpRequest"))//If the AJAX request response header will have, X-requested-with; 9. { response.setheader ("Sessionstatus", "timeout");//Set session state one at the response header . return false; . . } . return true; </span>
Thus, if the session times out and is an AJAX request, it will be in the response header, Sessionstatus has a timeout;
Then use a global method to process the session timeout to jump to the page.
jquery can be used $.ajaxsetup method, ext also has a similar method
<span style= "FONT-SIZE:18PX;" >1.//Global AJAX access, processing Ajax sesion Timeout 2. $.ajaxsetup ({ 3. ContentType: "Application/x-www-form-urlencoded;charset=utf-8", 4. Complete:function (xmlhttprequest,textstatus) { 5. var sessionstatus=xmlhttprequest.getresponseheader ("Sessionstatus"); Get the response head through XMLHttpRequest, Sessionstatus, 6. if (sessionstatus== "timeout") { 7. If the timeout is processed, specify the page 8 to jump to . Window.location.replace ("${path}/common/login.do"); 9. } One. } ); </span>
Summary:A qualified engineer, can provide a timely solution to the system bug, but a good engineer, never stop here, he will analyze the pros and cons of each scheme, choose a more elegant solution, or provide such a solution!
When we have adapted to the title of qualified engineers, are we moving towards a good engineer?
Java Engineering Accumulation--a solution to the failure of authorization validation in Ajax