打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
DWR中取得session等信息(转) - 阳光e站------Sunsoft Team ...
关键字: dwr

DWR Java API

There are only 2 Java classes that you commonly need to depend on within DWR as a user - WebContext and WebContextFactory. In DWR 1.x these are in the uk.ltd.getahead.dwr package, for DWR 2.0 onwards they are in org.directwebremoting. These classes give you to access to the standard HTTP servlet objects:

    * HttpServletRequest
    * HttpServletResponse
    * HttpSession
    * ServletContext
    * ServletConfig

You use WebContext like this:

import uk.ltd.getahead.dwr.WebContext;
import uk.ltd.getahead.dwr.WebContextFactory;
///
WebContext ctx = WebContextFactory.get();
req = ctx.getHttpServletRequest();

It is important that you treat the HTTP request and response as read-only. While HTTP headers might get through OK, there is a good chance that some browsers will ignore them (IE ignores cache pragmas for example) Any attempt to change the HTTP body WILL cause DWR errors.

WebContext uses a ThreadLocal variable so you can use the line above anywhere in your code (so long as it has been fired off by DWR).

See also the JavaDoc for DWR in general, or the specific page for WebContext.

WebContext replaces ExecutionContext which is deprecated as of DWR 1.1.
Alternative Method

It is possible to get access to the HTTP servlet objects without writing code that depends on DWR - just have the needed parameter (i.e. HttpServletRequest, HttpServletResponse, HttpSession, ServletContext or ServletConfig) declared on your method. DWR will not include it on the generated stub and upon a call of the method it will fill it in automagically.

For example if you have remoted a class like this:

public class Remote {
  public void method(int param, ServletContext cx, String s) { ... }
}

Then you will be able to access it from Javascript just as though the ServletContext parameter was not there:

Remote.method(42, "test", callback);

DWR will do the work of filling in the parameter for you.

There is one slight caveat with this method. You should ensure you are not using the 'callback function as first parameter' idiom, instead use the 'callback as last parameter' or 'callback in meta-data object' idioms. See the scripting introduction

----------------------------------------------------------------------

最近我们的项目决定采用Ajax技术实现一些功能,最后采用了DWR.在使用DWR的时候,遇到了要获取session中保存的用户信息的问题.上网查了一下资料,测试成功后简单记录起来.

在DWR中需要用到session,request等这些东西的时候,可以用以下方法获取:
1. 使用DWR的API (很多人都不推荐这种做法,经测试,使用起来肯定没问题)
WebContext ctx = WebContextFactory.get();
ctx.getSession()
ctx.getHttpServletRequest()
...

2. 在Java的服务方法中定义这些类型的参数,让DWR自动传入相应的对象:
HttpServletRequest,HttpServletResponse,HttpSession,ServletContext,ServletConfig.
示例如下:
Java端定义方法: saveOrder(String id, String name, HttpSession session)
Javascript调用: OrderService.saveOrder(jsID,jsName,callBack)
注意,这里不需要传入session这个参数,DWR会自动传入,在Java端可以直接使用. 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Struts2操作Request、Session和Application对象 - juanming5612的日志 - 网易博客
Servlet的多线程安全问题
DWR学习笔记(四)
servlet的三大作用域对象和jsp的九大内置对象及其四大作用域对象
servlet 多线程安全
J2EE笔记(二) — Java web
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服