deploying spring mvc web app to tomcat: classpath issue - eclipse

I am developing a small web app using Spring MVC framework. Basically, the app provides web interface, where user can upload XML file and verify it against specific XSD file. I put my XSD file within "src/main/webapp/XSDfoler". I put this folder into my Tomcat's classpath. (I am using Tomcat embedded into my Eclipse). In my code, in order to access my XSD file I simply used
ClassPathResource("myXSD.xsd");
It works fine. Now, I created a .war file from my webapp and tried to deploy it to another standalone Tomcat. When trying to run it, it give NullPointer exception since it cannot locate "myXSD.xsd" file. So as I get it, I have to somehow include this file into classpath of this standalone Tomcat instance. I looked for some nice step by step tutorial or article explaining how to deploy webapp to a standalone Tomcat server after doing the development in IDE with embedded Tomcat. Could anyone please explain/help. Thanks!

Try
ServletContext context = httServletRequest.getSession().getServletContext();
InputStream is = context.getResourceAsStream("/XSDfoler/myXSD.xsd");
alternatively use getResource() instead of getResourceAsStream()

Related

Running servlet within Eclipse requires libs to be defined 3 times - am I doing something wrong?

Hullo - issue is this:
I wrote a servlet in Eclipse which requires mysql-connector-java-5.1.22-bin.jar
To compile I need to add the jar via the project's "Java Build Path"
To deploy I need to add the jar to the project's "Deployment Assembly"
To run the servlet within eclipse I need to add the jar to the servlet's Run Configuration -> Classpath
It's not the end of the world re-re-repeating myself like this, but it does seem odd.
Given that Eclipse gets a lot of other stuff correct I'm guessing / hoping that maybe I'm overlooking some feature to avoid this silliness (I cannot imagine a scenario where you'd benefit from entering this in 3 different spots ... but maybe I'm being uncreative here ...).
Insights appreciated :-)
The only thing you need to do is to drop the jar in WebContent/WEB-INF/lib.
You are developing a Java Web project, so the traditional place to put the required libs (JAR files etc) is under /WEB-INF/lib. And you do it only once.
In Eclipse, when you create Dynamic Web Project the appropriate project structure is generated for you (this is a development structure). In this case you place your JAR files in ProjectName/WebContent/WEB-INF/lib folder. And this folder is *automatically included in the project's build path.
Considering the fact that it is a Java Web project (you said you use servlets) you have to deploy your web app to some Application Server, like GlassFish, JBoss, WebLogic, WebSphere etc, or more simple Web Container like Apache Tomcat. If you do this thru Eclipse, then again your web project is automatically deployed.
NB!
There may be some additional details related to using libraries.
For instance, when it comes to using database drivers (MySql, PostgreSQL, Oracle etc) Tomcat advises the following while configuring JNDI Datasource (quote):
Before you proceed, don't forget to copy the JDBC Driver's jar into
$CATALINA_HOME/lib
In your case (MySQL) see the example here: MySQL DBCP Example
Also see my answer related to Webapp configuration file organization convention.
Hope this will help you.
P.S. Here is a step-by-step example: How do I access MySQL from a web application?

java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool

I am tring to configure a dynamic web project using eclipse helos. I use tomcat 6. When I add the libraries to the WebContent/lib it works fine. But I want to a folder called lib in the project's root level(root/lib). When I put my library files to that folder it builds fine. Run fine to the login page. When I try to log in it gives
"java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool"
Please help me with this.
You're creating a web app and deploying it as a WAR file in a Java EE container. That means that your deployment must follow the WAR standards. Put your JARs in WEB-INF/lib. The container will find them there.
Tomcat 6 and 7 have changed things up so JDBC driver JARs need to go in the server /lib directory. The app server expects to find them with its class loader. Try putting the MySQL JDBC connector JAR in /lib and see if that helps.

How to deploy Java web application project from Eclipse to live Tomcat server?

I have developed an web application using HTML, Java Servlet and all. While developing I was using Tomcat to deploy it in order to test it.
Now my development is done and I want to make it live. For that we have live server but as I am new to all this I dont know how to deploy my java web application on live server?
So please help me if you know to answer?
My Project Structure
ProjectName
->src
->beanClass
->class1
->Class2
->easyServlet
->Servlet1
->Servlet2
->Servlet3
->easyTrans
->Class1
->Class2
->Class3
->Class4
->build
->WebContent
->META-INF
->MENIFEST.mf
->WEB-INF
->lib(contain javascript files)
->web.xml
->html1
->html2
->html3
->html4
->html5
I am also using MySql so what I have to about it..
You will have to build a WAR of the project.
You can do this
in eclipse: right click on the project, Click "Export", and choose war file in the dialog (and mention, the destination, name and all)
via ant using the war task
The ant option is better because when you have multiple developers on the project and the code is in version control, it is easier to get the project automatically (using ant) and build a war. (you have version control, don't you?)
But this is more of an operational difference (albeit an important one) but the war created in either way are same
Deploy the war to the server
You can manually copy the war file to the $TOMCAT_HOME/webapps directory (See the "Creating and Deploying a WAR File" section on this article)
You can use the Tomcat 6 "Manager" application.
Update
You said that you are using MySql also. MySql should be installed on a server (it can be on the same server) and the configuration should be changed (username, password, server details) so that the application connects to the same database (I am sure you are not hard coding database details and credentials in your application and reading them from some configuration, this is the configuration that has to be changed)
For that we have live server but as I am new to all this I dont know how to deploy my java web application on live server?
I assume by this you meant , you have a public IP assigned to a server. Now you can install tomcat into this server and open the tomcat port for public and you will be able to access.
Now build a war file of your webapplication and put it into web-apps dir of the tomcat and start the server
Making a few assumptions here. You need
A tomcat instance running on your production server
Permissions to make changes to the tomcat instance
A war file that bundles your application
If you have both, then you need to navigate to the Tomcat manager page and follow the instructions to upload your war file.
Deploy the war to the server
You can manually copy the war file to the $TOMCAT_HOME/webapps directory.
You can use the Tomcat 6 "Manager" application.

Deploy GWT to Tomcat (servlet not running)

What is the correct way to deploy a GWT app to Tomcat? I have made a GWT app with server side code (servlets). It works in Hosted mode but only when I copy the WAR folder (after compiling) to Tomcat webapp directory and rename the war folder correctly.
My GWT app servlet is in URI /mygwtapp, so I renamed the folder mygwtapp. The app loads correctly with the problem that servlet do not run i.e. /mygwtapp/servlet does not run.
All the libraries needed by the server side code are in the WEB-INF/lib folder. What could be the reason for this?
Thanks.
By default, Tomcat serves an app named 'mygwtapp' from the context path '/mygwtapp'. (Whereas the GWT built-in jetty serves it from the context path '/'.)
Your servlet paths will usually be '/mygwtapp/*'. This means, that in conjunction with the context path, your servlets are now accessible from '/mygwtapp/mygwtapp/*'. (Try it: Simply enter the full URL in your browser - the Servlet will usually complain that something is missing, or that it doesn't support GET, but you'll know for sure now, where it lives.)
So you have two options:
Tell the client side to call the servlets at '/mygwtapp/mygwtapp/*' (I think, this is already taken care of automatically when using the #RemoteServiceRelativePath annotation)
Adjust the Context path of the web application in Tomcat, as explained in http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
One other option is to use GWT.getModuleBaseURL() then in your client side code append servlet name. This will make it work on jetty or Tomcat without any special config.

how to implement sample jms, springs program in my eclipse

I'm new to jms and springs and eclipse as well. i'm trying to implement jms example using Springs in MyEclipse 7.1, and i have been referring the following url.
http://java-x.blogspot.com/2006/12/implementing-jms-with-spring-messaging.html
But Iam unable to understand how to execute the program, it contains a servlet but does not have any html or jsp page, should i create a web project for it or java application? How do i run the program.
Thanks and Best Regards.
You should create a web project to hold the servlet. A web project will have a directory for java source and a directory for web content (images, html, jsp, etc.). Make sure that you put the servlet code inside the java source directory. You will also need to register the servlet in web.xml unless it is a Java EE 5 servlet with annotations.
To run your servlet, right click on it and select Run As -> Run on Server