天天看點

DWR2中調用Servlet相關類(簡單例子)

DWR2一大特性就是在DWR中可以通路WEB上下文的SERVLET,REQUEST等,主要用到的類是WebContext,WebContextFactory,使用WebContextFacotry.get()方法得到目前的WebContext,原理是将WebContext與目前thread綁定,内部使用ThreadLocal來維護。

use case:

在IE中使用JAVASCRIPT來判斷制定使用者名經是否已經登陸。

client(index.jsp)

DWR2中調用Servlet相關類(簡單例子)

<% @ page language = " java "   import = " java.util.* "  pageEncoding = " gb2312 " %>

DWR2中調用Servlet相關類(簡單例子)

<%

DWR2中調用Servlet相關類(簡單例子)

String path  =  request.getContextPath();

DWR2中調用Servlet相關類(簡單例子)

String basePath  =  request.getScheme() + " :// " + request.getServerName() + " : " + request.getServerPort() + path + " / " ;

DWR2中調用Servlet相關類(簡單例子)

// 為示範:假設已經登入成功

DWR2中調用Servlet相關類(簡單例子)

request.getSession( true ).setAttribute( " cur_user " , " jack " );

DWR2中調用Servlet相關類(簡單例子)

%>

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.01 Transitional//EN " >

DWR2中調用Servlet相關類(簡單例子)

< html >

DWR2中調用Servlet相關類(簡單例子)

   < head >

DWR2中調用Servlet相關類(簡單例子)

     < base href = " <%=basePath%> " >

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

     < script type = ' text/javascript '  src = ' dwr/engine.js ' >   </ script >

DWR2中調用Servlet相關類(簡單例子)

     < script type = ' text/javascript '  src = ' dwr/util.js ' >   </ script >

DWR2中調用Servlet相關類(簡單例子)

     < script type = ' text/javascript '  src = ' dwr/interface/UserCheck.js ' >   </ script >

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

     < script language = " javascript " >

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

        function checkUserIsLogin(name)  ... {

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

            UserCheck.checkUserIsLogin(name,function(res) ...{

DWR2中調用Servlet相關類(簡單例子)

                    var retMsg = ( true == res ) ? "已經登入" : "未登入";

DWR2中調用Servlet相關類(簡單例子)

                    DWRUtil.setValue("msg",retMsg);

DWR2中調用Servlet相關類(簡單例子)

                }

DWR2中調用Servlet相關類(簡單例子)

            );

DWR2中調用Servlet相關類(簡單例子)

        }

DWR2中調用Servlet相關類(簡單例子)

     </ script >

DWR2中調用Servlet相關類(簡單例子)

   </ head >

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

   < body >

DWR2中調用Servlet相關類(簡單例子)

    < input type = " text "  name = " username "   />< input type = " button "  value = " check "  onclick = " checkUserIsLogin(document.all.username.value) "   /> 目前session中儲存為jack

DWR2中調用Servlet相關類(簡單例子)

    < br />

DWR2中調用Servlet相關類(簡單例子)

    < div id = " msg " ></ div >

DWR2中調用Servlet相關類(簡單例子)

   </ body >

DWR2中調用Servlet相關類(簡單例子)

</ html >

DWR2中調用Servlet相關類(簡單例子)

服務端:UserCheckUtil.java

DWR2中調用Servlet相關類(簡單例子)

package  com.dwr;

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

import  javax.servlet.http.HttpSession;

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

import  org.directwebremoting.WebContext;

DWR2中調用Servlet相關類(簡單例子)

import  org.directwebremoting.WebContextFactory;

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

public   class  UserCheckUtil  ... {

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

    public boolean checkUserIsLogin(String username) ...{

DWR2中調用Servlet相關類(簡單例子)

        WebContext ctx = WebContextFactory.get();

DWR2中調用Servlet相關類(簡單例子)

        HttpSession session = ctx.getSession(false);

DWR2中調用Servlet相關類(簡單例子)

        if (null != session && username.equals(String.valueOf(session.getAttribute("cur_user"))))

DWR2中調用Servlet相關類(簡單例子)

            return true;

DWR2中調用Servlet相關類(簡單例子)

        else

DWR2中調用Servlet相關類(簡單例子)

            return false;

DWR2中調用Servlet相關類(簡單例子)

    }

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

}

配置檔案:dwr.xml

DWR2中調用Servlet相關類(簡單例子)

<? xml version="1.0" encoding="UTF-8" ?>

DWR2中調用Servlet相關類(簡單例子)

<! DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd" >

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

< dwr >

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

   < allow >

DWR2中調用Servlet相關類(簡單例子)

     < create  creator ="new"  javascript ="UserCheck" >

DWR2中調用Servlet相關類(簡單例子)

       < param  name ="class"  value ="com.dwr.UserCheckUtil" />

DWR2中調用Servlet相關類(簡單例子)

     </ create >

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

   </ allow >

DWR2中調用Servlet相關類(簡單例子)
DWR2中調用Servlet相關類(簡單例子)

</ dwr >

DWR2中調用Servlet相關類(簡單例子)

繼續閱讀