`
huangxiaoke20
  • 浏览: 42484 次
  • 来自: 福建福州
最近访客 更多访客>>
社区版块
存档分类
最新评论

在DWR调用的java代码中使用Session,Request,Response等

阅读更多

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

分享到:
评论

相关推荐

    DWR中取得session等信息.doc

    在 DWR 中取得 Session 等信息可以使用两种方法:使用 DWR 的 API 或者在 Java 服务方法中定义参数。后者是推荐的做法,因为它更简洁、更易于维护。无论使用哪种方法,都是为了获取用户信息,以便更好地实现业务逻辑...

    DWR与Jsp内置对象关系

    例如,通过DWR调用的方法可以决定是否在session中设置特定的标志,从而影响后续JSP页面的行为。 3. **错误处理**:DWR调用的服务器端方法如果抛出异常,可以捕获并将异常信息返回给客户端,此时可以利用DWR的错误...

    dwr中文帮助文档

    它简化了AJAX(Asynchronous JavaScript and XML)的使用,使得开发者可以轻松地调用服务器端的Java方法,并将结果直接在浏览器中显示。DWR通过JSON或XML数据格式传输信息,提供了安全、高效的数据交换机制。 **...

    JSP+Servle+Ajaxt的供应商管理系统(完整版)

    开发者可以使用JSP的内置对象如request、response、session和application等来处理HTTP请求和响应,以及管理用户的会话状态。 **Servlet**: Servlet是Java EE中用于处理HTTP请求的Java类。在JSP+Servlet应用中,...

    java知识点.pdf

    例如,在Java中,可以通过private关键字将类的属性私有化,然后提供公共的getter和setter方法来访问这些属性。 - **继承**: 继承允许创建一个新的类,该类继承了现有类的特性。这有助于代码的复用和扩展。Java中的...

    java知识点汇总学习路线与笔记

    - **TCP/UDP**:Java中的网络编程主要涉及两种协议:传输控制协议(TCP)和用户数据报协议(UDP)。 - **AWT、事件模型、匿名类**:AWT是Java早期的图形用户界面工具包,事件模型描述了如何处理用户的输入事件;匿名类是...

    DWR3 访问WEB元素的两种方法实例详解

    DWR (Direct Web Remoting) 是一种开源的Java框架,它允许JavaScript在浏览器端与服务器端的Java对象进行交互,从而实现动态的Web应用程序。在DWR3中,访问WEB元素主要有两种方法,这两种方法都能有效地帮助开发者在...

    中软笔试题.pdf

    在子类中调用此方法时,将执行子类的版本。 4. **JSP中的静态include与动态include** - **静态include**使用`<jsp:include page="...">`,它在编译时就将包含的页面合并,不会检查被包含页面的更新,适用于静态...

    采用ajax实现的图片验证码

    采用ajax技术实现的图形验证码,在前端进行验证。验证码信息为图片。每一行代码均有注释,通俗易懂。 实现步骤: 1、创建web工程 ... 6、在js文件中,通过dwr,调用AjaxService类中的方法,取得数据,实现判断

Global site tag (gtag.js) - Google Analytics