How to get ServletContext in ring call function

I am using Clojure + Ring to create a web application that runs on Glassfish 3. How do I access a variable ServletContext

in a Ring function init

?

+3


source to share


1 answer


ServletContext, if any, is available on the request card. It was helpful for me to see the values :context, :servlet-context

and :servlet-context-path

. Here's a little glue ring that I use to define the path:



(def ^:dynamic *app-context* nil)

(defn wrap-context [handler]
 (fn [request]
  (when-let [context (:context request)]
    (logging/debug (str "Request with context " context)))
  (when-let [pathdebug (:path-debug request)]
    (logging/debug (str "Request with path-debug " pathdebug)))
  (when-let [servlet-context (:servlet-context request)]
    (logging/debug (str "Request with servlet-context " servlet-context)))
  (when-let [servlet-context-path (:servlet-context-path request)]
    (logging/debug (str "Request with servlet-context-path " servlet-context-path)))
  (binding [*app-context* (str (:context request) "/")]
     (logging/debug (str "Using appcontext " *app-context*))
     (-> request
         handler))))

(defn url-in-context [url]
    (str *app-context* url))

      

+1


source







All Articles