ITeye博客

DWR访问Servlet API

平民王子 · 2010-12-02 17:07 · DWR · Web前端 · Cache · JavaEE · DWR · JavaScript · Servlet · Markdown

DWR提供两种方式访问Servlet API:

  1. 使用WebContext类,WebContext是由WebContextFactory产生的,它提供一个静态方法get()获得

  2. 直接访问Servlet API 即在方法传参为HttpSession

访问Servlet API(使用WebContext)  web.xml文件配置 <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>  Java处理类 package com.lbx.dwr.servlet; import org.directwebremoting.WebContextFactory; public class AddSession { //该方法用于将name参数添加为一个HttpSession属性 public void addSession(String name){ //通过WebContextFactory访问Servlet API WebContextFactory.get().getSession(true).setAttribute("username", name); } }  dwr.xml文件的配置 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "http://getahead.org/dwr/dwr30.dtd"> <dwr> <allow> <create creator="new" javascript="Demo"> <param name="class" value="com.lbx.dwr.servlet.AddSession"/> </create> </allow> </dwr>  客户端jsp代码 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>访问Servlet API(使用WebContext)</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type='text/javascript' src='dwr/interface/Demo.js'></script> <script type='text/javascript' src='dwr/engine.js'></script> <script type='text/javascript' src='dwr/util.js'></script> </head> <body> <h3> 访问Servlet API </h3> 请输入您的名字 <input id="name" name="name" type="text" /> <br /> <input type="button" value="添加session属性" onclick="Demo.addSession(dwr.util.getValue('name'));" /> <div id="sessionMsg"></div> </body> </html>  测试jsp代码,看是否将值放到Session中去了 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>访问Servlet API</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> ${sessionScope.username} <br> <%=request.getSession().getAttribute("username") %> </body> </html>

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。