现在的位置: 首页 > 综合 > 正文

DWR操作servlet API

2013年02月01日 ⁄ 综合 ⁄ 共 843字 ⁄ 字号 评论关闭

一、使用Webcontext类

DWR提供了两个工具类:WebContext和WebContextFactory,其中WebContextFactory是产生WebContext的工厂类,一旦获得了WebContext类,就可以调用它的方法

(1)HttpServletRequest getHttpServletRequest():访问request对象

(2)HttpServletResponse getHttpServletResponse():访问response对象

(3)ServletConfig getServletConfig():访问ServletConfig对象

(4)ServletContext getServletContext():访问ServletContext对象

(5)HttpServlet getSession():访问当前请求关联的HttpSession对象

(6)HttpSession getSession(boolean flag):当前请求没有关联的session时,如果create 参数为true,则创建一个新的sessiion后返回;如果create参数为false,则返回null.

  如:public void addSession(String name){

WebContextFactory.get().getSession().setAtrribute("user",name);

}

二、直接访问Servlet API

public void addSession(String name,HttpSession sess){

    sess.setAttribute("user",name);

}

对然服务器类的处理方法中增加了httpSession参数,但该服务器类暴露成JavaScript实例时,addSession()方法将没有HttpSession参数。就是说,依然使用如下方式来调用addSessiion()方法:

add.addSession('yeeku');

抱歉!评论已关闭.