Details
-
New Feature
-
Resolution: Won't Fix
-
Major - P3
-
None
-
None
-
None
Description
It doesn't exist "official" support for using MongoDB in Web Apps. I attach my Java code with my idea to help support for Web. To use it, declare the Servlet Context Listener in the web.xml like this :
----------------------------------------------
<listener>
<listener-class>com.mongodb.web.MongoServletContextListener
</listener-class>
</listener>
<context-param>
<param-name>mongoURI</param-name>
<param-value>mongodb://localhost:27017</param-value>
</context-param>
----------------------------------------------
This listener :
- on the contextInitialized : initialize the static variable Mongo of the MongoProvider with the "mongoURI" param-value.
- on the contextDestroyed : close the connection of the static variable Mongo
Once this listener is started, you can use MongoProvider.getMongo() in any code (Servlet, Filter, Services, etc) to use Mongo.