1.spring得到servletContext,这个和session没有什么关系,上下文可以说是一个session容器,一个上下文可以有多个会话session
在web.xml中有以下配置后。加入spring对上下文的监听,
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
在后台用一下代码就可以得到javax.servlet.ServletContext
ContextLoader.getCurrentWebApplicationContext().getServletContext()
2.cxf得到session的方法,这个仅仅是为了测试每次webservice请求session不一样
package com.gaeainfo.tclgb.ws; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionContext; import javax.xml.ws.WebServiceContext; import javax.xml.ws.handler.MessageContext; import org.apache.cxf.transport.http.AbstractHTTPDestination; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.springframework.stereotype.Component; import org.springframework.web.context.ContextLoader; @Component(value="loginManagerWs") public class UserManagerWebServiceImpl implements UserManagerWebService { //会自动注入,配置了cxf监听之后 @Resource private WebServiceContext context; @Override public String showUserInfo() { /*String userName = (String) session.getAttribute("userName"); String depName = (String) session.getAttribute("depName"); Document doc = DocumentHelper.createDocument(); Element elt = doc.addElement("root").addElement("userName").addText(userName).addElement("depName").addText(depName); String result = doc.asXML();*/ MessageContext ctx = context.getMessageContext(); HttpServletRequest request = (HttpServletRequest) ctx.get(AbstractHTTPDestination.HTTP_REQUEST); System.out.println(request.getSession().getId()); return ""; } }