swfupload在xp系統上360極速模式報302錯誤解決方法
阿新 • • 發佈:2019-01-30
swfupload有時在谷歌或火狐核心丟失session,有攔截器驗證登入時會報302錯誤。
解決思路是在前臺獲取jsessionid,上傳時一併傳入後臺,在攔截器通過請求的jsessionid獲取session,再進行登入驗證。
1.前端通過<input type="hidden" id="ssid" value="<%=request.getSession().getId()%>"/>
獲取jsessionid,在post_params: {"jsessionid":document.all("ssid").value}
傳入引數。
2.後臺通過sessionid獲取session,使用session監聽器配合一個靜態的hashmap即可實現。
首先,建立自己的sessionContext
public class MySessionContext {
private static MySessionContext instance;
private HashMap<String,HttpSession> sessionMap;
private MySessionContext() {
sessionMap = new HashMap<String,HttpSession>();
}
public static MySessionContext getInstance () {
if (instance == null) {
instance = new MySessionContext();
}
return instance;
}
public synchronized void addSession(HttpSession session) {
if (session != null) {
sessionMap.put(session.getId(), session);
}
}
public synchronized void delSession(HttpSession session) {
if (session != null) {
sessionMap.remove(session.getId());
}
}
public synchronized HttpSession getSession(String sessionID) {
if (sessionID == null) {
return null;
}
return sessionMap.get(sessionID);
}
}
然後建立session監聽,要實現HttpSessionListener介面
public class SessionListener implements HttpSessionListener {
private MySessionContext myc = MySessionContext.getInstance();
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
myc.addSession(session);
}
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
HttpSession session = httpSessionEvent.getSession();
myc.delSession(session);
}
}
接著,在web.xml中配置session監聽器
<listener>
<listener-class>SessionListener</listener-class>
</listener>
完事,大功告成,之後在程式碼中直接獲取就OK了
MySessionContext myc= MySessionContext.getInstance();
HttpSession sess = myc.getSession(sessionId);
3.在攔截器中使用session判斷登入狀態就ok了