1. 使用DWR的API (很多人都不推荐这种做法,经测试,使用起来肯定没问题)
WebContext ctx = WebContextFactory.get();
ctx.getSession()
ctx.getHttpServletRequest()
...
@H_403_14@
HttpServletRequest,HttpServletResponse,HttpSession,ServletContext,ServletConfig.
示例如下:
Java端定义方法: saveOrder(String id,String name,HttpSession session)
Javascript调用: OrderService.saveOrder(jsID,jsName,callBack)
注意,这里不需要传入session这个参数,DWR会自动传入,在Java端可以直接使用.
@H_403_14@
在使用DWR时经常有人找不到如何取session,因为Bean的有些方法是要在登陆情况下才能使用,所以网上有很多人想了其它的方法,比如使用过滤器来处理等等,这里我讲的是使用DWR自身的方法来取到,代码如下:
@H_403_14@
@H_403_14@
源码复制打印
import uk.ltd.getahead.dwr.WebContext;
import uk.ltd.getahead.dwr.WebContextFactory;
...
public String getSessionId() {
WebContext webContext = WebContextFactory.get();
return webContext.getSession().getId();
}
...
@H_403_14@
import uk.ltd.getahead.dwr.WebContext; import uk.ltd.getahead.dwr.WebContextFactory; ... public String getSessionId() { WebContext webContext = WebContextFactory.get(); return webContext.getSession().getId(); } ...
@H_403_14@
如上代码,只为了取一个SessionId,其中有一个Factory是专来处理WebContext的,看到这里应该明白了
@H_403_14@
@H_403_14@
而WebContext的原型如下
@H_403_14@
源码复制打印
package uk.ltd.getahead.dwr;
public interface WebContext {
uk.ltd.getahead.dwr.Container getContainer();
javax.servlet.http.HttpSession getSession();
javax.servlet.http.HttpSession getSession(boolean b);
javax.servlet.ServletConfig getServletConfig();
javax.servlet.ServletContext getServletContext();
javax.servlet.http.HttpServletRequest getHttpServletRequest();
javax.servlet.http.HttpServletResponse getHttpServletResponse();
java.lang.String forwardToString(java.lang.String string) throws javax.servlet.ServletException,java.io.IOException;
java.lang.String getVersion();
@H_403_14@
package uk.ltd.getahead.dwr; public interface WebContext { uk.ltd.getahead.dwr.Container getContainer(); javax.servlet.http.HttpSession getSession(); javax.servlet.http.HttpSession getSession(boolean b); javax.servlet.ServletConfig getServletConfig(); javax.servlet.ServletContext getServletContext(); javax.servlet.http.HttpServletRequest getHttpServletRequest(); javax.servlet.http.HttpServletResponse getHttpServletResponse(); java.lang.String forwardToString(java.lang.String string) throws javax.servlet.ServletException,java.io.IOException; java.lang.String getVersion();
@H_403_14@
可以看到所有你想要的HttpServletRequest,HttpSession 都可以在这里取得到
@H_403_14@
那他是如果工作的呢,看了一下代码,我能理解是:
当DWR初始化时: DWRServlet ->AbstractDWRServlet.init(..)
在init中 WebContextFactory.setWebContextBuilder(builder);
当一个client 访问 DWRServlet ->AbstractDWRServlet.doPost(...) 时
builder.set(req,resp,getServletConfig(),getServletContext(),container);
这里线程更新了WebContextBuilder
所以在WebContextFactory中get时取到的都是当前用户的所请求的相关信息(session,request,response)
@H_403_14@
你能理解了吗:)
不理解没关系,你只要记得在DWR的Bean中使用
@H_403_14@
WebContext webContext = WebContextFactory.get();
webContext.getSession() ;
webContext.getHttpServletRequest() ;
webContext.getHttpServletResponse();
@H_403_14@
WebContext webContext = WebContextFactory.get(); webContext.getSession() ; webContext.getHttpServletRequest() ; webContext.getHttpServletResponse();