这是讲述如何通过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;
});
}
[1] [2] 下一页