• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

cannot initiate Spring context in Web Servlet

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys. It's... been awhile since I post here. I'm trying some Spring Framework, right now, but I ran into trouble.

See, this is my bean.xml



And this is my SpringUtil, where I call on my beans



And this is the line I use to call it, for example:

User user = SpringUtil.getUserService().validate("Admina", "pass123");



When I use this in normal Java (public static void main), it works perfectly. But if I use it on a Servlet, I gets this error:



Is there something I missed? What should I add here? I'm using Spring Framework 4.2.4, by the way.

Thanks for the help! I'll appreciate that!
 
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like your WEB-INF/lib folder in the deployed location is missing spring jars. Add them and it should be gone.
 
Kristian Wijaya
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Adding the jars to that folder work wonder. However, then I get this kind of error



Is there something I should do? I'm not sure how the process of adding WebApplicationContext works, so help will be appreciated. Thanks!
 
Anurag Verma
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In eclipse, its okay if you have it in classpath (Though it is always recommended to have it in WEB-INF/lib for a non-maven/non-gradle web project). When you run it outside eclipse in some web or app server, you must have it in WEB-INF/lib. Did you check the lib folder in deployed location? is it having the jars??
 
Kristian Wijaya
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anurag Verma wrote:In eclipse, its okay if you have it in classpath (Though it is always recommended to have it in WEB-INF/lib for a non-maven/non-gradle web project). When you run it outside eclipse in some web or app server, you must have it in WEB-INF/lib. Did you check the lib folder in deployed location? is it having the jars??



Do you mean the JAR containing WebApplicationContext? If that is the case, yes I have. It's in the Spring-web-4.2.4.RELEASE.jar.

This is my WEB-INF/lib folder contents:

antlr-2.7.7.jar
aopalliance-.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.7.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
junit-3.8.1.jar
ojdbc6.jar
spring-aop-4.2.4.RELEASE-javadoc.jar
spring-aop-4.2.4.RELEASE-sources.jar
spring-aop-4.2.4.RELEASE.jar
spring-aspects-4.2.4.RELEASE-javadoc.jar
spring-aspects-4.2.4.RELEASE-sources.jar
spring-aspects-4.2.4.RELEASE.jar
spring-beans-4.2.4.RELEASE-javadoc.jar
spring-beans-4.2.4.RELEASE-sources.jar
spring-beans-4.2.4.RELEASE.jar
spring-context-4.2.4.RELEASE-javadoc.jar
spring-context-4.2.4.RELEASE-sources.jar
spring-context-4.2.4.RELEASE.jar
spring-context-support-4.2.4.RELEASE-javadoc.jar
spring-context-support-4.2.4.RELEASE-sources.jar
spring-context-support-4.2.4.RELEASE.jar
spring-core-4.2.4.RELEASE-javadoc.jar
spring-core-4.2.4.RELEASE-sources.jar
spring-core-4.2.4.RELEASE.jar
spring-expression-4.2.4.RELEASE-javadoc.jar
spring-expression-4.2.4.RELEASE-sources.jar
spring-expression-4.2.4.RELEASE.jar
spring-instrument-4.2.4.RELEASE-javadoc.jar
spring-instrument-4.2.4.RELEASE-sources.jar
spring-instrument-4.2.4.RELEASE.jar
spring-instrument-tomcat-4.2.4.RELEASE-javadoc.jar
spring-instrument-tomcat-4.2.4.RELEASE-sources.jar
spring-instrument-tomcat-4.2.4.RELEASE.jar
spring-jdbc-4.2.4.RELEASE-javadoc.jar
spring-jdbc-4.2.4.RELEASE-sources.jar
spring-jdbc-4.2.4.RELEASE.jar
spring-jms-4.2.4.RELEASE-javadoc.jar
spring-jms-4.2.4.RELEASE-sources.jar
spring-jms-4.2.4.RELEASE.jar
spring-messaging-4.2.4.RELEASE-javadoc.jar
spring-messaging-4.2.4.RELEASE-sources.jar
spring-messaging-4.2.4.RELEASE.jar
spring-orm-4.2.4.RELEASE-javadoc.jar
spring-orm-4.2.4.RELEASE-sources.jar
spring-orm-4.2.4.RELEASE.jar
spring-oxm-4.2.4.RELEASE-javadoc.jar
spring-oxm-4.2.4.RELEASE-sources.jar
spring-oxm-4.2.4.RELEASE.jar
spring-test-4.2.4.RELEASE-javadoc.jar
spring-test-4.2.4.RELEASE-sources.jar
spring-test-4.2.4.RELEASE.jar
spring-tx-4.2.4.RELEASE-javadoc.jar
spring-tx-4.2.4.RELEASE-sources.jar
spring-tx-4.2.4.RELEASE.jar
spring-web-4.2.4.RELEASE-javadoc.jar
spring-web-4.2.4.RELEASE-sources.jar
spring-web-4.2.4.RELEASE.jar
spring-webmvc-4.2.4.RELEASE-javadoc.jar
spring-webmvc-4.2.4.RELEASE-sources.jar
spring-webmvc-4.2.4.RELEASE.jar
spring-webmvc-portlet-4.2.4.RELEASE-javadoc.jar
spring-webmvc-portlet-4.2.4.RELEASE-sources.jar
spring-webmvc-portlet-4.2.4.RELEASE.jar
spring-websocket-4.2.4.RELEASE-javadoc.jar
spring-websocket-4.2.4.RELEASE-sources.jar
spring-websocket-4.2.4.RELEASE.jar
xml-apis-1.0.b2.jar



Anything I might have missed? I'm thinking that maybe I missed the Apache Commons jars, I dunno which one I need to add though. Help?

I also would like to ask what line should I put in the Servlet to work on the WebApplicationContext. Will need help here.
 
Anurag Verma
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have the listener configured in web.xml? if not, add this into your web.xml -
 
Kristian Wijaya
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anurag Verma wrote:Do you have the listener configured in web.xml? if not, add this into your web.xml -



Tried to add that, yes. But after that there's always an error in my server startup, usually NoClassDefFound like the the ones found in Commons.

Like this one: org.apache.commons.dbcp.BasicDataSource
 
Anurag Verma
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats a good sign, keep the listener in your web.xml and keep adding the libraries it needs. the class you mentioned is in commons-dbcp. Its really time consuming to add jars one by one, why don't you give a try to Maven and make your life a little easy. it will take care of adding all the subsequent libraries your project will need.
 
Kristian Wijaya
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have never started using Maven. So excuse me if I'm going to ask a lot:

First, what does it do and how does it benefit me?

Second, how do I set it up?

Third, after I finished setting it up, what needs to be done in my project in order to implement it? Extra: Do I have to re-create my project and make it more like Maven in order to implement it?

This is going to be long, so I'm gonna have to ask you to take your time. I haven't used Maven so I don't know how this'll work.
 
Anurag Verma
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
install m2e plugin in eclipse (can be done using eclipse marketplace)
Create a maven project with archetype "maven-archetype-webapp"
once the project is created, have all your source code in src/main/java, resources (xml file) in src/main/resources
open pom.xml, add dependencies node and inside it add all your dependencies one by one.
for adding dependencies tags search for your library(eg spring-mvc) in mvnrepository website, select the version you want and the dependency xml node you need in maven will appear, copy it to your pom.
on saving the pom.xml file, whatever dependencies are not present on your machine will be downloaded from internet, have patience, first time it takes more time depending on your internet speed.
to have a deployable war file, right click on your project and select Run As -> Maven Install.
deploy your war to tomcat (it will have all your jars well placed.

Hope that helps. One Important thing, for trying out something new, always be ready for new challenges
 
Kristian Wijaya
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so I'm trying to create that Maven project. Now I'm a little confused on this part: Group ID and Artifact ID. To answer to this whole commons problem, what do you think the Group ID and Artifact ID setting should be?
 
Anurag Verma
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
group Id should be like words reversed of your organization domain, artifact Id is project name, for example, Spring mvc module has "org.springframework" as group id and spring-webmvc as artifact id. To start with you can have any value you want, which can be your name too.
 
Kristian Wijaya
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So far working nice and right now I'm in process of putting on my source code in src/main/java. But... well...

First lemme give you the structure of the src/main/java


-com
--dao
---impl
----UserDAOImpl.java
---UserDAO.java
--factory
---SpringUtil.java (this is to call the main xml)
---bean.xml (Application Context goes here)
--model
---User.java
---User.hbm.xml
--services
---impl
----UserServiceImpl.java
---UserService.java
--servlet
---LoginServlet.java



Then this is UserDAO and its Impl





Now for some reason, the validate function wants me to get rid of the @Override or else it produces an error. Last time before I integrated Maven, it worked perfectly. So... what went wrong here?

Second question, should that bean.xml go to src/main/resources instead?
 
Anurag Verma
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check how to change jdk version of project from pom, that'll solve the override issue, and yes xml should go into resources with the package you already have
 
Kristian Wijaya
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let me see if I get this right. This is the pom.xml I got after some modification.



It doesn't seem to solve the problem. Where did I go wrong? If I go cmd java -version, it would give out:

java version "1.8.0_66"
Java(TM) SE Runtime Environment (build 1.8.0_66-b18)
Java HotSpot(TM) 64-Bit Server VM (build 25.66-b18, mixed mode)

So...?

Also, that bean.xml. Do I have to recreate the packaging in the resources folder first, or...?

-------------

EDIT: For backup case, I decided to have the non-Maven project ongoing (I'll transfer to Maven later when I solve the Override problem above). Turns out, all I needed were just commons-dbcp and commons-pool, and after that, no more error. Still there's this line during the Tomcat startup that I wonder if it'll be bad:

WARN: HHH000342: Could not obtain connection to query metadata : Cannot load JDBC driver class 'com.microsoft.sqlserver.jdbc.SQLServerDriver'



What kind of bad thing do you think will happen?

Secondly... now that I do that, how do I put the WebApplicationContext command on my servlet? What code do I need to write so I don't get this error anymore (It should be up there, but here's a reminder)

 
Anurag Verma
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Add your Spring dependencies into the jar as already mentioned earlier in steps to use maven. (every time you get a class not found, look for the maven dependency of it in mvnrepository and add it to pom.xml

replace your plugin node in pom.xml with this for referring correct java version-


We should not be using group id of some other organization for our own applications. you can probably have it as com.kristian or just kristian. Using the groupId of other organizations is technically correct, but ethically wrong.
 
Anurag Verma
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
replace 1.6 with the version of java you want to work on
 
Kristian Wijaya
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be clear, my version is 1.8.0_66. Do I write it like that or just 1.8?
 
Anurag Verma
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just have 1.8 there.
 
Kristian Wijaya
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remind me, what were you referring to when you said this:

Add your Spring dependencies into the jar as already mentioned earlier in steps to use maven. (every time you get a class not found, look for the maven dependency of it in mvnrepository and add it to pom.xml



Is it about the warning "Cannot load JDBC driver class 'com.microsoft.sqlserver.jdbc.SQLServerDriver'"?

Also I tried to change the plugin node with your suggestion, but the problem is not going away. The problem is that they want to remove the Override tag, or else it's considered an error, it's like the interfaces aren't linked at all.

Is there something I missed?
 
Kristian Wijaya
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At any point, I think I have solved my problem in the non-Maven version. However I am still interested on how to utilize that Maven thing, so maybe I should move this to another topic?

The cannot load JDBC thing was solved by putting on the SQLJDBC jar to the proper places.

Let me know what you think before I consider this topic solved and then move on to another topic. Because this will be about Maven overall.
 
Anurag Verma
Ranch Hand
Posts: 171
Hibernate Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instead of adding the jar, you should be adding a dependency in pom for every missing library, maven will take care of adding all other libraries which are needed by the one you added. Also when you share the project you don't need to share any of the jars, just share the project folder, keep only the src folder and pom.xml, everything else will be created by the ide.
 
"To do good, you actually have to do something." -- Yvon Chouinard
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic