技术开发 频道

通过DWR使用JAVA的Session

  【IT168 文档】这是讲述如何通过DWR使用JAVA的Session的例子,想要通过DWR使用JAVA的Session,如果您还没有引入DWR的JAR包到工程中,那么首先引入DWR的JAR包到工程中,随后就可以使用了。在编写好正确的JAVA代码后,应该在dwr.xml发布要使用的JAVA类,随后重新启动JAVA服务器,下面是发布配置:

  <!-- DwrSession - DwrSession --> 
   <create creator="new" javascript="DwrSession">
  <param name="class" value="您完整的包名称.DwrSession"/>
  <include method="getSessionId"/>
  <include method="setSession"/>
  <include method="getSession"/>
  </create>

  JAVA类的代码:

   import uk.ltd.getahead.dwr.*;
  
public class DwrSession {
  
//得到session编号
  public String getSessionId() {   WebContext webContext = WebContextFactory.get();
  webContext.getSession();
  webContext.getHttpServletRequest();
  webContext.getHttpServletResponse();
  
return webContext.getSession().getId();
  }
  
public void setSession(String value,String sessionName){
  WebContext webContext
= WebContextFactory.get();
  
//设定session值
  webContext.getSession().setAttribute(sessionName, value);
  }
  
public String getSession(String sessionName){
  WebContext webContext
= WebContextFactory.get();
  
//返回session值
  return (String)webContext.getSession().getAttribute(sessionName);
  }
  }
0
相关文章