如何通过DWR使用JAVA的Session

原创|其它|编辑:郝浩|2009-12-30 10:05:11.000|阅读 673 次

概述:这是讲述如何通过DWR使用JAVA的Session的例子,想要通过DWR使用JAVA的Session,如果您还没有引入DWR的JAR包到工程中,那么首先引入DWR的JAR包到工程中,随后就可以使用了。

# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>

  这是讲述如何通过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);
  }
  }

  JSP页面代码:

  <%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>DWR使用session演示页面</title>
  <script type='text/javascript' src='/您的web工程/dwr/interface/DwrSession.js'></script>
  <script type='text/javascript' src='/您的web工程/dwr/engine.js'></script>
  <script type='text/javascript' src='/您的web工程/dwr/util.js'></script>
  <script language="javascript">
  //得到sesion编号
  function getSessionId(){
  //得到session编号
  DwrSession.getSessionId(function getdata(data){
  //在页面显示session编号
  document.getElementById("sessionTd").innerHTML = data;
  });
  }
 //设定session值,并同时显示设定的session值 
  function setSession(){
  //要设定的值
  var temp = document.getElementById("txtSession").value;
  //要使用的session名称
  var name = "test";
  DwrSession.setSession(temp,name);
  //得到设定的session值
  DwrSession.getSession(name,function getData(data){
  //在页面显示session值
  document.getElementById("getSessionTd").innerHTML = data;
  });
  };
  </script>
  </head>
  <body>
  <form id="form1" name="form1" method="post" action="">
  <table width="100%" border="1">
  <tr>
  <td><table width="100%" border="1">
  <tr>
  <td width="30%"><a href="#">getSessionId</a></td>
  <td width="70%"><table width="100%" border="1">
  <tr>
  <td><%
  out.println(session.getId());
  %>
  </td>
  </tr>
  <tr>
  <td id="sessionTd"> </td>
  </tr>
  </table></td>
  </tr>
  </table></td>
  </tr>
  <tr>
  <td><table width="100%" border="1">
  <tr>
  <td width="30%"><input name="txtSession" type="text" id="txtSession" size="15" />
  <input type="button" name="Submit" value="设定Session值"/></td>
  <td width="70%" id="getSessionTd"> </td>
  </tr>
  </table></td>
  </tr>
  </table>
  </form>
  </body>
  </html>


标签:

本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com

文章转载自:网络转载

为你推荐

  • 推荐视频
  • 推荐活动
  • 推荐产品
  • 推荐文章
  • 慧都慧问
扫码咨询


添加微信 立即咨询

电话咨询

客服热线
023-68661681

TOP