• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Integrating Spring in an existing servlets project

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have a servlet & jsp project which i need to extend with new functionality. Is it possible to integrate spring in this project just for the new functionality without affecting the other code?

Thanks
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Spring features do you want to use?

If you use just Servlets/JSPs, you will not fully access to power of dependency injection.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to use the dependency injection (this is out it seems) and spring mvc. I don't know how splitting the spring controllers definitions will impact the servlets' definitions that are already present in web.xml. I mean, for example, can I still use multiple deployment descriptors? Or what can I use?
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, so you want to use Spring Web MVC, right?
In that case, there is nothing to worry, you can use DI to Spring MVC controllers, this is not related to existing Servlets. There is no any impact to existing Servlets either.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First you need to bootstrap your ApplicationContext into your web application. This is necessary for Servlets or Spring MVC

In your web.xml you add the ContextLoaderListener and point to your application context xml.

You then can have your Servlet access an ApplicationContext

in your Servlet you would call

ApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());

That is all that is needed, now you can lookup your beans from your Servlet using context.getBean();

Most of the time the call to get the App context and your lookup are done in the init method of your Servlet.

Thanks is the "Dependency Injection" of a Spring class into your Servlet

Mark
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not planning to use servlets for the code that i'm supposed to implement, but spring controllers. I need to inject beans only in the controllers that i develop. So, is this setup still necesary?
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You just need to specify ContextLoaderListener in web.xml, it'll instantiate IoC container using applicationContext.xml.

If you don't want to use Servlets for new features, you wouldn't call the following:


context.getBean is dependency lookup.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean that only these lines i need to add in web.xml



and i can start using spring without any impact on the other code?
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, i have this error:



These are the first 4 lines in servlets.xml:


It seems the doctype (which works in tomcat btw) doesn't work on jboss. Is there any workaround for this?
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try the updated Spring 2.x DTD -



or the xmlns version -

 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No luck. Is there something else i must do besides having spring.jar in my buildpath and declaring the doctype/xmlns in xml?
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The issue seems to be firewall related. www.spring.... cannot be reached. Is there anyway i can validate the dtd's locally? I tried and i've put the dtd in my application's root, but the issue is still there.
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nick Potter wrote:



Change to the following:

If still have problem, try to debug ContextLoaderListener.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you need to access the DTD locally it should be -


and the DTD files should go in the /WEB-INF directory.
 
Nick Potter
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't use the SYSTEM... but i found a way for people who have this problem too. So to access the dtd locally, one must have it in workspace (i didn't put in the WEB-INF, i think it can be anywhere on the hard-drive, but it's probably bad) and have an XML Catalog validate it. For eclipse users it's easy, since it has a built-in XML Catalog in windows - preferences - xml. Should be pretty easy to use.
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic