简体   繁体   English

将会话值从操作传递到seam框架中的servlet

[英]Pass session values from action to servlet in seam framework

I am setting the session object in TestAction class and when i am trying to get the session object in TestServlet it returns null. 我在TestAction类中设置会话对象,当我尝试在TestServlet中获取会话对象时,它返回null。 Any one please tell me how to pass session from action class to servlet in Seam framework. 任何人都请告诉我如何在Seam框架中将会话从操作类传递到servlet。

@Scope(ScopeType.EVENT)
@Name("testAction  ")
public class TestAction  {

    public void setSessionObj(){

        FacesContext facesContext = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
        session.setAttribute("temp", "124563");
    }
}

//Servlet starts here // Servlet从这里开始

 public class TestServlet extends HttpServlet {

        public void init(ServletConfig servletConfig) throws ServletException {
            super.init(servletConfig);
            servletContext = servletConfig.getServletContext();

        }

        protected String doExecute(HttpServletRequest request,
                HttpServletResponse response) throws Exception {

                 Session session =    request.getSession(false);
            String user1 = (String) session .getAttribute("temp");
        }
    }

Below is the observation on debugging the session instances 以下是调试会话实例的观察结果

I checked session object instance in case of what i am getting in action and servlet, they both are different instances of Session. 我检查了会话对象实例,以防万一我正在行动和使用servlet,它们都是Session的不同实例。 For eg instance in action is StandardSession[41CBDED6EBBBECEBA001A70555F51CA5] and what im getting in servlet is StandardSession[EACBDED6E34BECEB3401A70555F51CA5], Any reason why i am getting different session instances 例如,正在运行的实例是StandardSession [41CBDED6EBBBECEBA001A70555F51CA5],而在servlet中即时获取的是StandardSession [EACBDED6E34BECEB3401A70555F51CA5],我得到不同会话实例的任何原因

There shouldn't be any need to access the session attributes via the FacesContext in your servlet. 不需要通过Servlet中的FacesContext访问会话属性。 Just use request.getSession() to get the session. 只需使用request.getSession()即可获取会话。 As long as your requests are actually from the same session, the session attributes should be available in the servlet. 只要您的请求实际上来自同一会话,会话属性就应该在servlet中可用。

I would use a proxy-object that gets injected via CDI / Spring into both the JSF Managed Bean and the servlet. 我将使用通过CDI / Spring注入到JSF托管Bean和Servlet中的代理对象。

This example creates an CDI Bean with is kept alive during the whole time and just stores a String. 本示例创建一个CDI Bean,该CDI Bean在整个时间内一直保持活动状态,并且仅存储一个String。 Trough dependecy injection both components can access it. 通过依赖注入,两个组件都可以访问它。

Create an empty bean.xml file under META-INF resource folder 在META-INF资源文件夹下创建一个空的bean.xml文件

@Named
@ApplicationScoped
public class Container{
 private String temp;

 public Container(){

 }
 public void setTemp(String temp) {
  this.temp = temp;
 }

 public String getTemp() {
  return temp;
 }
}

@Scope(ScopeType.EVENT)
@Name("testAction  ")
public class TestAction  {

 @Inject 
 Container container;

public void setSessionObj(){
 container.setTemp("123456");

}
}

//Servlet starts here // Servlet从这里开始

public class TestServlet extends HttpServlet {

@Inject
Container container; 
    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        servletContext = servletConfig.getServletContext();

    }

    protected String doExecute(HttpServletRequest request,
            HttpServletResponse response) throws Exception {

             Session session =    request.getSession(false);
        String user1 = container.getTemp();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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