银行对接SESSION丢失怎么办

注意:该操作是在servlet2.2以后得版本不支持原有的getSessionid

step1:添加session 监听器配置web.xml

<listener>
   <listener-class>
                com.keweisoft.ecmp.httpsessionlistener.ScsSeesionListener
    </listener-class>
</listener>


step2:编写监听器

package com.keweisoft.ecmp.httpsessionlistener;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;


public class ScsSeesionListener implements 
HttpSessionListener{
    private PayNotifySessionContext sessionContext=
            PayNotifySessionContext.getInstance();
    public void sessionCreated(HttpSessionEvent event) {
            HttpSession session=event.getSession();
            sessionContext.addSession(session);
    }

    public void sessionDestroyed(HttpSessionEvent envent) {
            HttpSession session=envent.getSession();
            sessionContext.removeSession(session);
            
    }

}

step3编写监听器对应操作

package com.keweisoft.ecmp.httpsessionlistener;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;

public class PayNotifySessionContext {
     
     private static PayNotifySessionContext sessionContext=new PayNotifySessionContext();
     private Map<String,HttpSession> sessionMap=new HashMap<String, HttpSession>();
     private PayNotifySessionContext()
     {
         
     }
     
     public static PayNotifySessionContext getInstance()
     {
         return sessionContext;
     }
     
     
     public synchronized void addSession(HttpSession session)
     {
         if(null!=session)
         {
             sessionMap.put(session.getId(), session);
         }
     }
     
     public synchronized void removeSession(HttpSession session)
     {
         if(null!=session)
         {
             sessionMap.remove(session.getId());
         }
     }
     
     public synchronized HttpSession getSession(String JSESSIONID)
     {
         
         HttpSession session =JSESSIONID==null?null:sessionMap.get(JSESSIONID);
         return session;
     }
}  


step4通过支付通知路径保存当前的JSESSIONID

//本例为支付宝 支付通知URL携带以下可能因支付而丢失的保存在SESSION中的值
    alipay.setReturn_url(SysConfigLoader.getSystemConfig().getProperty(
                        "returnUrl")
                        + "?JSESSIONID="
                        + JSESSIONID
                        + "&areaId="
                        + areaId
                        + "&payAreaId=" + payAreaId);
//支付成功之后那么该URL就会跨域请求你的服务器并携带了这些值你索要做的就是找到你的
session后替换到生成的新的session
this.getRequest().getSession().setAttribute(
                ".freemarker.Session",
                null == session.getAttribute(".freemarker.Session") ? session
                        .getAttribute(".freemarker.Session") : null);
        this.getRequest().getSession().setAttribute("user", session.getAttribute("user"));
        this.getRequest().getSession().setAttribute("randomKey",
                session.getAttribute("randomKey"));
        this.getRequest().getSession().setAttribute(
                "javax.servlet.jsp.jstl.fmt.request.charset",
                session.getAttribute("javax.servlet.jsp.jstl.fmt.request.charset"));

猜你喜欢

转载自liweichao.iteye.com/blog/2215102