找到一个ajax的小工具DWR,
现在有点麻烦的是
在Server端提供服务方法的那个类里 怎么取得当前web应用的相关数据
例如session
哎,烦死了,搞半天

解决方案 »

  1.   

    import org.directwebremoting.WebContext;
    import org.directwebremoting.WebContextFactory;        WebContext wctx = WebContextFactory.get();使用这个ctx应该就可以获取到session和request等
    /**
     * Class to enable us to access servlet parameters.
     * @author Joe Walker [joe at getahead dot ltd dot uk]
     */
    public interface WebContext extends ServerContext
    {
        /**
         * Get the script session that represents the currently viewed page in the
         * same way that an HttpSession represents a cookie.
         * @return A browser object for this user
         */
        ScriptSession getScriptSession();    /**
         * What is the URL of the current page.
         * This includes 'http://', up to (but not including) '?' or '#'
         * @return The URL of the current page
         */
        String getCurrentPage();    /**
         * Returns the current session associated with this request, or if the
         * request does not have a session, creates one.
         * @return Returns the http session.
         * @see HttpServletRequest#getSession()
         */
        HttpSession getSession();    /**
         * Returns the current HttpSession associated with this request or, if
         * there is no current session and create is true, returns a new session.
         * If create is false and the request has no valid HttpSession, this method
         * returns null.
         * @param create false to return null if there's no current session
         * @return the session associated with this request
         * @see HttpServletRequest#getSession(boolean)
         */
        HttpSession getSession(boolean create);    /**
         * Accessor for the http request information.
         * @return Returns the request.
         */
        HttpServletRequest getHttpServletRequest();    /**
         * Accessor for the http response bean.
         * <p>You can't use this request to directly reply to the response or to add
         * headers or cookies.
         * @return Returns the response.
         */
        HttpServletResponse getHttpServletResponse();    /**
         * An attribute used by {@link WebContext#forwardToString(String)} to inform
         * anyone that wants to know that this is a request from DWR.
         */
        public static final String ATTRIBUTE_DWR = "org.directwebremoting";    /**
         * Forward a request to a given URL and catch the data written to it.
         * It is possible to distinguish requests that arrive normally and requests
         * that come from a DWR forwardToString() by the presence of a request
         * attribute. Before the request is forwarded, DWR will call:
         * <pre>
         * request.setAttribute(WebContext.ATTRIBUTE_DWR, Boolean.TRUE);
         * </pre>
         * @param url The URL to forward to
         * @return The text that results from forwarding to the given URL
         * @throws IOException if the target resource throws this exception
         * @throws ServletException if the target resource throws this exception
         * @throws IllegalStateException if the response was already committed
         */
        String forwardToString(String url) throws ServletException, IOException;    /**
         * For system use only: This method allows the system to fill in the session
         * id and page id when they are discovered.
         * @param page The URL of the current page
         * @param scriptSessionId The session id passed in by the browser
         */
        void setCurrentPageInformation(String page, String scriptSessionId);
    }
      

  2.   

    确实很麻烦,不过,我建议你将操作jsp隐式对象的过程放在jsp里进行,如果需要某些参数(id,password等),在jsp取得后以参数方式传递过去。上面的兄弟是建议你用servletContext对象,我估计取不到这个对象。
      

  3.   


    //例子供参考
    public List getList(String name,ServletContext cx) {
    HttpServletRequest request = null;
    WebContext ctx = WebContextFactory.get();
    HttpSession s = ctx.getSession();
    request = ctx.getHttpServletRequest();
    String name = request.getParameter("name");
    }
      

  4.   

    为什么不把参数hidden在页面上调用dwr时作为参数传入service中呢?
      

  5.   

    终于知道了,我自己搞定了...
    通过WebContextFactory.get();方法
    取得一个WebContext的对象,就可以取得request和response了...接下的的就no problem了!!!!