Thanks this does seem to solve the problem of the servlet being
reinitialized on every run.

On Oct 10, 11:10 pm, Adrian Cuthbertson <adrian.cuthbert...@gmail.com>
wrote:
> Hi Dmitri,
>
> The problem is probably related to calling init with args. It requires
> that super() gets called - I can't remember where I saw the
> documentation, but here's an example of what works for me.
>
> The following is a generic servlet which gets passed a clojure
> namespace name as an init parameter at init time which is saved in an
> atom. Then on each service call, it parses the servlet path and uses
> the "ipath" (first component of the url after the context), along with
> the ns name from the atom to load a clj namespace (once) and call a
> function called <ipath> passing it the req and rsp. The clj fn cn then
> handle the method type, GET, POST, etc and has access to all the
> servlet stuff.
>
> This way you have a servlet as a gateway to a clj ns and the function
> called determined by the req url...
>
> (ns svlt.Svlt
>   (import (javax.servlet.http HttpServlet HttpServletRequest
>      HttpServletResponse HttpSession)
>     (javax.servlet ServletConfig)
>     )
>   (:gen-class :extends javax.servlet.http.HttpServlet
>        :state state :init clinit))
>
> (defn -clinit
>   []
>   [[] (atom (hash-map))])
>
> (defn -init-void
>   [this] ; NB - careful, must rather call init() (void) than with the cfg 
> args.
>            ; If with args, must call .super() which is problematic
>   (let [;cfg (.getServletConfig this)
>         ns-nm (.getInitParameter this "app-ns")]
>     (println :Svlt :init :ns-nm ns-nm)
>     (swap! (.state this) assoc :ns-nm ns-nm)))
>
> (defn -service
>   [this #^HttpServletRequest req #^HttpServletRequest rsp]
>   (let [cpath (.getContextPath req)
>         spath (.getServletPath req)
>         ipath (.getPathInfo req)
>         _ (println :Svlt :cpath cpath :spath spath :ipath ipath)
>         ns-nm (get @(.state this) :ns-nm)
>         _ (println :Svlt :ns-nm ns-nm)
>         _ (when (nil? ns-nm) (throw (java.io.IOException.
>               (str "No app-ns param found in Svlt config: " spath))))
>         ipath (if (or (nil? ipath) (= ipath "")) "root" ipath)
>         ipath (if (.startsWith ipath "/") (.substring ipath 1) ipath)
>         ns-sym (symbol ns-nm)
>         _ (println :ns-sym ns-sym :ipath-now ipath)
>         found-ns (find-ns ns-sym)
>         found-ns (if (nil? found-ns)
>                    (let [n (create-ns ns-sym)] (require ns-sym) n)
>                    found-ns)
>         _ (when (nil? found-ns) (throw (java.io.IOException.
>               (str  "Namespace not found for: " ns-sym))))
>         req-fn (get (ns-publics ns-sym) (symbol ipath))
>         ]
>     (req-fn req rsp)))
>
> -Hth, Adrian

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to