MongoDB and Apache Tomcat for a Webapp

5,784 views
Skip to first unread message

theo...@freenet.de

unread,
Aug 30, 2013, 12:59:30 PM8/30/13
to mongod...@googlegroups.com
Hello,
i will build a webapplication with MongoDB and Apache Tomcat.
I searched in the group and for the internet, but i don't find a solution to build a webapplication with mongoDB and Apache Tomcat.
I find this site: http://plindenbaum.blogspot.de/2010/09/using-mongodb-with-apache-tomcat.html
But i don't understand that.

I have install the Java API on Eclipse and connected to the MongoDB and tested some basic operationen like read, delete update and search.
Now i want to link MongoDB with Apache Tomcat.
Can anyone help me?

Thanks for the help
Marius

Keith Branton

unread,
Aug 30, 2013, 3:28:50 PM8/30/13
to mongod...@googlegroups.com
Hi Marius,

Tomcat needs to be able to load the java driver. How you do this will depend on how you are running your tomcat application. You could copy the mongo java driver to the lib directory of your tomcat install, or if you are using maven and the sysdeo eclipse plugin like I do, by adding it to your pom.

Once tomcat can see the driver then you should be able to get the examples on that page you linked working.

There is also a pretty helpful "getting started" section in the java driver docs http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started-with-java-driver though that is not specific to tomcat.

Hope it helps,

Keith.


--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

theo...@freenet.de

unread,
Aug 30, 2013, 5:59:46 PM8/30/13
to mongod...@googlegroups.com
Thank you Keith. I find the "getting started" link you shared in your post and made this complete, before i posted this topic.

Russell Bateman

unread,
Aug 30, 2013, 6:14:27 PM8/30/13
to mongod...@googlegroups.com
Marius,

I'm not sure to have understood the end of your answer. Had you already looked at that before you asked the question and Keith's answer wasn't sufficient for you, or are you okay now?

Do you need more suggestions assembling your applications toolstack between Tomcat and MongoDB?

Many of us are Java/JEE guys in this forum and can give you more suggestions on that though, at this point, it's less a MongoDB question than a general JEE one until you get down to having trouble getting the Java driver to connect, do your MongoDB CRUD in your DAO, etc.

Just ask!

Cheers,

Russ

theo...@freenet.de

unread,
Sep 3, 2013, 6:37:15 AM9/3/13
to mongod...@googlegroups.com
Thanks for your post Russell,
yes, i am at the beginning of java developing. I just develope java but this was for 3 years and only for one year.
I install tomcat and the sysdeo plugin for eclipse. This works!
I also install maven and the m2e plugin for eclipse. I don't know if its works, because i find no documenatation for m2e and the getting statet guide from apache maven give me a error in the command line by executing the commands to create a project.
Maven don't find the prom.xml file. I think this file would create by creating a maven project.
Any idea?


Thanks for the help
Marius



Russell Bateman

unread,
Sep 3, 2013, 10:18:45 AM9/3/13
to mongod...@googlegroups.com
Sorry, Marius. I'm not a Maven guy. I use Ivy/ant. I would try Eclipse's web tool platform forum or stackoverflow.com for this.

theo...@freenet.de

unread,
Sep 6, 2013, 5:31:56 AM9/6/13
to mongod...@googlegroups.com
Hello Keith,
i used the maven and the sysdeo eclipse plugin. Have you any "How to"-Sites e.g. Learning-Sites for using this tools. I found the maven homepage and read the getting started guide. It is good, but i need more informations.
Can you tell me what i must write in the pom.xml to connect my java application to the tomcat.
I know, that i use the key-words <...> for the pom.xml.
Have you an example or something else or information for this, that i can read?


Thanks for the help
Marius

Am Freitag, 30. August 2013 21:28:50 UTC+2 schrieb Keith Branton:

Keith Branton

unread,
Sep 7, 2013, 12:09:18 AM9/7/13
to mongod...@googlegroups.com
Hi Marius,

So there are two things here - using maven with eclipse, then getting sysdeo working with a mavenized project in eclipse.

I use m2e with the m2e-wtp adapter (though I'm not sure if the adapter is required for sysdeo), and set up a profile in my pom called m2e that sets the output folders to bin instead of target. To do this I add...

<properties>
<target.dir>target</target.dir>
<target.dir.classes>target/classes</target.dir.classes>
<target.dir.testClasses>target/test-classes</target.dir.testClasses>
</properties>

<profiles>
<profile>
<id>m2e</id>
<properties>
<target.dir>bin</target.dir>
<target.dir.classes>bin</target.dir.classes>
<target.dir.testClasses>bin</target.dir.testClasses>
</properties>
</profile>
</profiles>

...to the pom (actually I usually put this in a parent pom since I normally build multi-module maven projects) and then add...

        <build>
<directory>${basedir}/${target.dir}</directory>
<outputDirectory>${basedir}/${target.dir.classes}</outputDirectory>
<testOutputDirectory>${basedir}/${target.dir.testClasses}</testOutputDirectory>
</build>

...to use these destinations. These all go in the <project> section of your pom. Once m2e is enabled you will have a Maven Dependencies library in the project explorer and an M badge on your project.

For a tomcat project the packaging type in the pom should be war.

To get sysdeo working you need to make sure the appropriate devloader jar is copied to the tomcat lib folder as per the instructions, then configure it on the project properties -> tomcat section. 

On the general tab, I check all 4 checkboxes, set Context name to / and set the web app root to src/main/webapp
On the DevLoader Classpath tab check all the non-test resources, including the bin folder of the war project
On the export to war settings I do nothing because maven makes my war files.

Then with any luck you should be able to start tomcat and it should all just work. If you need any more help, just ask.

Remember if you add any new non-test dependencies in future you need to update the DevLoader Classpath tab to include them or you'll get a class not found error when tomcat tries to access them.

Regards,

Keith.

theo...@freenet.de

unread,
Sep 11, 2013, 11:05:38 AM9/11/13
to mongod...@googlegroups.com
Okay thank you.

But i think i don't use Maven.
I want to connect the MongoDB on the Tomcat. The MongoDB statert from the mongod.exe (to have a seperate instanz) and the code for the connection is java in eclipse with the java api driver for mongoDB. I Used this for a applikation and it works. Now i want this on tomcat.
I made a short example with jsp and javabeans without mongoDB and it works. When i postet the code for the MongoDB Connection and i get a exception.

type Exception report

message javax.servlet.ServletException: java.lang.NoClassDefFoundError: meinbeanpackage/MeineJavaBean

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: meinbeanpackage/MeineJavaBean
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

javax.servlet.ServletException: java.lang.NoClassDefFoundError: meinbeanpackage/MeineJavaBean
	org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
	org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
	org.apache.jsp.HalloJSPmitJavaBean_jsp._jspService(HalloJSPmitJavaBean_jsp.java:87)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)


I write the code for mongodb in the javabean and select the javaean in my jsp-File.
Is there a problem if i connect my to the mongodb in the public function of my javabean or ist this for the mongoDB okay? The packe name is: meinebeanpackage and the Bean-Class-Name is: MeineJavaBean.java

regrads
Marius

Justin Lee

unread,
Sep 11, 2013, 11:14:35 AM9/11/13
to mongod...@googlegroups.com
There isn't a mongo problem here.  Your war file is missing that class and the JSP can't find it.

Keith Branton

unread,
Sep 11, 2013, 1:04:23 PM9/11/13
to mongod...@googlegroups.com
OK - I thought you were using maven, but it doesn't make a huge difference to getting this going.

If that stack trace was the result of running tomcat using the sysdeo plugin then it looks like you most likely didn't include your project's classes in the sysdeo devloader config.

theo...@freenet.de

unread,
Sep 11, 2013, 6:22:32 PM9/11/13
to mongod...@googlegroups.com
I don't use the sysdeo plugin. Can you tell me in which folder i must submit the mongodb jar api driver to run this project. I think this is the missing class. Because without mongodb it works. I have build this project as Dynamic Web Project.
Actually the lib is in: | Java Resources
                                  |--- Libaries

theo...@freenet.de

unread,
Sep 11, 2013, 6:53:14 PM9/11/13
to mongod...@googlegroups.com
Hello,
i solved the problem. The program hasn't find a jar file in my libaries.
But now i have a new problem. he doesn't find the MongoClient class.
Here is the stack trace:

type Exception report

message javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mongodb/MongoClient

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mongodb/MongoClient
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/mongodb/MongoClient
	org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
	org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
	org.apache.jsp.HalloJSPmitJavaBean_jsp._jspService(HalloJSPmitJavaBean_jsp.java:87)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

java.lang.NoClassDefFoundError: com/mongodb/MongoClient
	meinbeanpackage.MeineJavaBean.getDateString(MeineJavaBean.java:22)
	org.apache.jsp.HalloJSPmitJavaBean_jsp._jspService(HalloJSPmitJavaBean_jsp.java:76)
	org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

I had import the mongo-java-driver-2.11.2.jar in the lib directory. This jar-File has the package com.mongodb. In this package ist the class MongoClient.
Can you tell me the problem or rather where i must include the jar file that tomcat find the class.

regards
Marius

Russell Bateman

unread,
Sep 11, 2013, 7:02:58 PM9/11/13
to mongod...@googlegroups.com
Marius,

None of this has anything to do with MongoDB.

You should consider putting your questions to the Eclipse web tool
platform forum instead and/or I would be willing to answer some of them.
I do Java/JEE applications with Tomcat principally and, obviously, I use
MongoDB. (I'm at UTC-7, so expect questions to be viewed accordingly.)

It's just that we shouldn't continue to congest this form with questions
that don't relate to MongoDB.

Cheers,

Russ

Keith Branton

unread,
Sep 11, 2013, 7:12:12 PM9/11/13
to mongod...@googlegroups.com
If you copy mongo-java-driver-2.11.2.jar to the lib folder in your tomcat installation, then restart tomcat, tomcat should be able to see it.

It's not an optimal approach but it should work.

theo...@freenet.de

unread,
Sep 12, 2013, 5:01:32 AM9/12/13
to mongod...@googlegroups.com
Thank you this solved the problem.

theo...@freenet.de

unread,
Sep 12, 2013, 7:20:09 AM9/12/13
to mongod...@googlegroups.com
I have a new problem. This is not a mongodb problem. So can you tell my in which folder, group, hompage i can wirte this problem.
The problem is about java server faces (jsf).

I used this example: http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.jst.jsf.doc.user%2Fhtml%2Fgettingstarted%2Ftutorial%2FJSFTools_tutorial_JSF20.html
And first it works. Now i have a HTTP 404The requested resource is not available.  error. I uninstall the tomcat Windows Service and install the zip Version, because with the Windows Service Version i must rename the ports, because i get the error "these ports are already used". I also changed the ports in the zip Version of tomcat to test this, but i have the same error. So i changed it to the orgnial prot numbers (8005, 8080, 8009)
Then i run the Tomcat with the startup.bat outside the localhost:8080 works
Then i run the Tomcat in eclipse with a jsp with javabean project the project works, but the localhost:8080 have the same error.
Then i run the Tomcat in eclipse with the jsf project i get the HTTP 404 error.

Can you tell my why the jsp Projects work but not the localhost. And the jsf-Project doesn't work?

regrads
Marius

Russell Bateman

unread,
Sep 12, 2013, 10:55:11 AM9/12/13
to mongod...@googlegroups.com
Please post to the Eclipse web tools platform forum at eclipse.org.

theo...@freenet.de

unread,
Sep 16, 2013, 6:13:55 AM9/16/13
to mongod...@googlegroups.com
i solved the problem.
Thanks for your helps.
Reply all
Reply to author
Forward
0 new messages