当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015计算机二级考试指导:通过DWR使用JAVA的Session
发布时间:2010/3/10 15:43:24 来源:城市学习网 编辑:MOON
  这是讲述如何通过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" /
  titleDWR使用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
  tdtable 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
  tdtable 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
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved