Spring MVC controller not invoked by Tomcat - spring

Update: Somehow, after another round of adjustments and redeployment, localhost:8080/ping-1.0/ping started working. Configuration files are still as below. I wish I knew what I fixed without knowing it, but it is solved now.
I've been wrestling with this for a couple days, tried all sorts of solutions I've seen here and elsewhere, and nothing has worked. I have a Spring MVC controller deployed in Tomcat, but can't access it.
Tools:
Spring 3.2.0
Tomcat 7
Java 1.6
Spring controller:
#Controller
public class PingController {
#RequestMapping("/ping")
public String ping (Model model) throws Exception {
System.out.println("ping ping ping");
String s = (new Date()).toString();
model.addAttribute("message", s);
return "ping";
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>ping</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/ping-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ping</servlet-name>
<url-pattern>/ping</url-pattern>
</servlet-mapping>
</web-app>
ping-servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="myclass.ping"/>
<mvc:annotation-driven />
</beans>
The WAR file is called ping-1.0.war. Deployment seems to go fine. I see a directory called ping-1.0 in $CATALINA_BASE/webapps and this in catalina.log:
INFO: Mapped "{[/ping],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String myclass.ping.PingController.ping(org.springframework.ui.Model) throws java.lang.Exception
Tomcat is running on port 8080. I can access localhost:8080/manager for instance. But localhost:8080/ping returns a 404 message from Tomcat. I see nothing in the logs other than a record of a GET request. No errors at all. I've tried a lot of variations of request mapping, URL filter, etc. and just can't get this to work.

You don't have the context root on your URL and you actually need to have /ping/ping because both the dispatcher servlet and your ping controller are mapped to /ping. Try this:
http://localhost:8080/ping-1.0/ping/ping

#RequestMapping("/ping")
Means /ping relative to the URL the dispatcher servlet listens on.
<url-pattern>/ping</url-pattern>
Here comes the problem. This makes your dispatcher servlet listen to one URL and one URL only. And that is assumingly localhost:8080/ping-1.0/ping. But your controller method is relative to that, so it would be localhost:8080/ping-1.0/ping/ping, and the disptacher servlet does not react on that URL. You have to use a pattern:
<url-pattern>/ping/*</url-pattern>
Now the dispatcher servlet can listen on all URLs starting with localhost:8080/ping-1.0/ping.
One final note: Depending on your configuration it could be that you have to omit ping-1.0.

I think you've configured Spring incorrectly.
I'd expect the servlet to be Spring's dispatcher servlet, not your ping controller. That's what figures out where to route the request. You don't have a front controller servlet.
I could be thinking Spring 2.x and earlier. I'll admit that I'd be incorrect if Spring 3.x changed the need for the dispatcher. But that's the way my applications are set up.

Related

org.springframework.web.servlet.DispatcherServlet noHandlerFound WARNING: No mapping for GET /DemoSpringMaven/add

When i try to run this project i received following error
"org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /DemoSpringMaven/add".
Error
org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping for GET /DemoSpringMaven/add
Web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>frontcontroller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>frontcontroller</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
WEB-INF/frontcontroller-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.fazaal"></context:component-scan>
</beans>
Controller/AddController.java
package com.fazaal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
#Controller
public class AddController {
#RequestMapping("/add")
public void add() {
System.out.println("I am Here!");
}
}
First, all these web.xml, servlets' XML configurations, manual setting of chains - this is the last age, sorry. Spring hid all these configurations inside own framework and you can use just very convenient annotations. One application class, one configuration class - and the web application works! And if you are a newbie in Spring, Spring MVC or JSP/Servlets, or you need to start your application ASAP, or you need a platform from which you want to start the framework learning, just use Spring Boot.
Second, if you want to leave your configuration ... how will MVC know about mapping /DemoSpringMaven/add? You specified just /add. Add full mapping or add global controller #RequestMapping("/DemoSpringMaven").
Try this servlet mapping:
<servlet-mapping>
<servlet-name>dispatcher-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Also the method should have #ResponseBody before it (probably this it the source of the issue):
#ResponseBody
#RequestMapping("/add")
public void add() {
System.out.println("I am Here!");
}
But as Dmitry Ionash said, this is really old way how to build Spring web applications. Try Spring Boot: https://spring.io/guides/gs/rest-service/

Accessing Spring Rest Service without Dispatcher Servlet

Here actually i am trying to access my spring based rest full service, I am not configuring DispatcherServlet in web.xml, instead of that i am using ContxtLoaderListener to load my spring configuration file.
From my logs i can see my service is getting initialized, when ever i access the above url, ICallServlet is receiving the request since it has the url-pattern as '/*'(this i can't modify).
Here my problem is i could not able to access my service, request is not reaching my service. without using DispatcherServlet is there any way to invoke my rest service, Some one please help me to resolve this issue.
I have a Rest Controller :
package mypackage;
#RestController
#RequestMapping("/api/casaOnboarding")
public class CasaOnboardingRestService {
#ResponseBody
#RequestMapping(value="/pwebXML", method=RequestMethod.POST, consumes={"application/json", "application/xml"})
public ResponseEntity pwebXML(#RequestBody OnboardingReq onboardingReq,
HttpServletRequest request, HttpServletResponse response){
System.out.println("Request Reached");
----
}
}
Web.xml (No Dispatcher Servlet)
<?xml version="1.0" encoding="UTF-8"?>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:controllerServiceContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>iCallUI</servlet-name>
<servlet-class>com.ui.ICallServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>iCallUI</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
controllerServiceContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd">
<context:annotation-config />
<context:component-scan base-package="mypackage"/>
<task:annotation-driven />
</beans>
Log File
10:45:41,643 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (ServerService Thread Pool -- 62) Creating shared instance of singleton bean 'casaOnboardingRestService'
10:45:41,643 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (ServerService Thread Pool -- 62) Creating instance of bean 'casaOnboardingRestService'
10:45:41,643 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (ServerService Thread Pool -- 62) Eagerly caching bean 'casaOnboardingRestService' to allow for resolving potential circular references
10:45:41,643 DEBUG [org.springframework.beans.factory.support.DefaultListableBeanFactory] (ServerService Thread Pool -- 62) Finished creating instance of bean 'casaOnboardingRestService'
URL:
http://localhost:8080/icall-ui/api/casaOnboarding/pwebXML
I'm sorry, but you can't dispatch spring mvc views without a Dispatcher Servlet. Your context will be loaded via the ContextLoaderListener, but just as you've discovered, your routes will never be called.
You could do something like mapping the dispatcher servlet to your api endpoints and then map iCallUI to catch the default route "/" as opposed to "/*":
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>iCallUI</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
ICallServlet will replace the default servlet and this may or may not have bad effects depending on how your application is set up. Static file serving may break, for example.
Subclassing org.springframework.web.servlet.DispatcherServlet is an option. But not knowing what you do in com.ui.ICallServlet, who knows how difficult it will be to extend DispatcherServlet.
Also, it seems like the long way around. If you are using Spring to declare your api routes, why not use it to declare them all? Why have two dispatching mechanisms? If you need to do some preprocessing per request then use a Servlet Filter.
Lastly, and perhaps the simplest solution. Just point iCallUI to another url pattern like: "/ui/*".
That pretty much exhausts the possibilities :). Well that and the fact that your controllerServiceContext file isn't set up to parse the url mapping. You also need to add
<mvc:annotation-driven />
Don't forget all the xml namespace info for that!
xmlns:mvc="http://www.springframework.org/schema/mvc"
.
.
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
.
Finally i got to know that there is no way (as per my knowledge) of invoking spring rest services without using DispatcherServlet.
Thank you so much #Robert for your valuable suggestions. As per #Robert Comments, I modified my code like below to get it to work.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:controllerServiceContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>iCallUI</servlet-name>
<servlet-class>com.ui.ICallServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>iCallUI</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="mypackage"/>
<mvc:annotation-driven />
</beans>
ControllerServiceContext.xml
I removed below lines of code and left as it is with old code (this file contains some other stuff related to the project).
<context:component-scan base-package="mypackage"/>
<task:annotation-driven />
Log file
After seeing the below statement in logs, I can say my service is ready to serve requests.
15:12:01,782 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] (ServerService Thread Pool -- 58) Mapped "{[/api/casaOnboarding/pwebXML],methods=[POST],params=[],headers=[],consumes=[application/json || application/xml],produces=[],custom=[]}" onto public org.springframework.http.ResponseEntity mypackage.CasaOnboardingRestService.pwebXML(mypackage.OnboardingReq,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
URL -
I used below url to access the service
http://localhost:8080/icall-ui/api/api/casaOnboarding/pwebXML
By using filter in web.xml
<!-- Spring security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

HTTPSessionListener using Spring injections and Services => impossible to access my beans

I work on a web app using Spring annotations to inject my Controllers and Services. In the app, I have users working on projects but I need to make sure one project is only edited by one user at a time so I have an attribute in the table "Project" in the database to save if the project is open and by whom.
I need to add an HTTPSessionListener to be able to "close" the project when the user editing it disconnects. It means that at the "sessionDestroyed" event, I want to call my DAO service to update the project in the database.
The only problem is that this service is injected by Spring and I cannot get it...
I tried to use #Autowired in my HTTPSessionListener but it didn't work, I tried to do like in this solution (Spring – How to do dependency injection in your session listener ) but I get a nullPointerException for the WebApplicationContext...
My web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:jsp="http://java.sun.com/xml/ns/javaee/jsp"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- jsp config => automatic inclusions in all jsp files -->
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<include-prelude>taglibs.jsp</include-prelude>
<include-prelude>setLanguage.jsp</include-prelude>
</jsp-property-group>
</jsp-config>
<display-name>MEANS</display-name>
<!--Spring dispatcher servlet -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern><!-- detect all urls ending with ".do" -->
</servlet-mapping>
<!-- The welcome files -->
<welcome-file-list>
<welcome-file>connection.jsp</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>30</session-timeout><!-- the session is automatically disconnected after 30 min of inactivity -->
</session-config>
<listener>
<listener-class>myPackages.listener.MySessionListener</listener-class>
</listener>
And the dispatcher-servlet.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- Spring MVC Support for annotations (JSR-303) -->
<mvc:annotation-driven/>
<!-- Spring View resolver => find the jsps -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:suffix=".jsp" />
<!-- Spring info : what packages to scan for detecting annotations -->
<context:component-scan base-package="myPackages"/>
So I need your help... Does anybody have an idea of how I could inject a Spring Service into my HTTPSessionListener?
Thanks in advance for your help!
One of the options (perhaps the best one from the design point of view) is to create a bean of scope session (don't forget to declare RequestContextListener) and put logic associated with the session lifecylce into it. Spring will automatically call its destruction method (annotated with #PreDestroy or configured as destroy-method) when session is to be destroyed.
Your approach with injection into session listener causes problems because you only have DispatcherServlet, not ContextLoaderListener, and WebApplicationContextUtils cannot obtain an application context associated with DispatcherServlet. If you choose to follow that apporach you should create a root application context and extract some of your beans into it, then you will be able to access it from session listener via WebApplicationContextUtils.

Redirecting from a legacy Servlet to Spring 3 and from Spring 3 to a legacy Servlet

I'm learning Spring by integrating Spring 3 into a legacy Servlet application and gradually converting the legacy app over.
A web.xml *-servlet.xml similar to the ones I am using are posted below. Basically things are set up such that retrieving a string like "search", Spring will route it to a Controller and the view resolver will convert "search" into "/jsp/search.jsp"
I ran into problems doing a response.sendRedirect("search") from a legacy Servlet and a legacy ServletFilter To Spring. The URL came out correctly, but I got a blank page despite System.out.println() calls indicating that the JSP was reached. No error messages from Spring and the browser only told me something went wrong with the redirect.
I fixed that problem by forwarding, instead of redirecting from the legacy Servlets and ServletFilters:
request.getRequestDispatcher("search").forward(request,response);
getServletConfig().getServletContext().getRequestDispatcher("search").forward(request,response);
Going in the OTHER direction from a new JSP done in Spring 3 to a legacy Servlet, I have buttons on screens so I just used a javascript call to "location.href=/helloworld". I will need to send some parameters, so I will likely convert those buttons into submitting tiny HTML forms.
What I am wondering is, is there a better approach to getting Spring 3 and the Legacy Servlets communicating in a better way.
Legacy Servlet => Spring 3
and
Spring 3 => Legacy Servlet
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>Acme</display-name>
<!--welcome-file-list>
<welcome-file>/login</welcome-file>
</welcome-file-list-->
<servlet>
<servlet-name>acme</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>acme</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- Help Find The Spring Config Files -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/nsd-servlet.xml,
/WEB-INF/nsd-security.xml
</param-value>
</context-param>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Integrate A Legacy Screen Done With A Servlet -->
<servlet>
<servlet-name>HelloWorldServlet</servlet-name>
<servlet-class>
com.legacy.HelloWorldServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorldServlet</servlet-name>
<url-pattern>/helloworldservlet</url-pattern>
</servlet-mapping>
</web-app>
My acme-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.acme.controller" />
<mvc:resources mapping = "/**" location = "/,file:/apps1/bea/user_projects/domains/acme/common/,file:/c:/weblogic_common_files/acme/"/>
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/jsp/"/>
<property name = "suffix" value = ".jsp"/>
</bean>
</beans>
When you use response.sendRedirect(String url) you are essentially passing in a URL that you want to redirect to.
sendRedirect observes the following rules:
/*
* Destination, it can be any relative or context specific path.
* if the path starts without '/' it is interpreted as relative to the current request URI.
* if the path starts with '/' it is interpreted as relative to the context.
*/
String destination ="/jsp/destination.jsp";
response.sendRedirect(response.encodeRedirectURL(destination));
When you type in "search", this is relative to the current request URI. Thus, unless your current request was to /acme/something, and assuming /acme/search is your servlet, the request will fail.
However, if you put the path relative to the root, using /acme/search, then the request will work from any context.
With that said, I'm not convinced this is really the best approach, as a redirect involves sending a response back to the client browser telling it to once again fetch content from yet another URL. This seems like a wasted trip to and from the server.
A better method may be to just wire in your servlets into Spring using something like Spring MVC. It's pretty flexible in that you can wrap new controller classes around your existing servlets and then invoke them directly by passing in the HttpServletRequest and HttpServletResponse objects. Once done, you can then slowly eliminate anything redundant with a working, efficient system instead of one chained together with redirects.
package samples;
public class SampleController extends AbstractController {
private int cacheSeconds;
// for wiring in cacheSeconds
public void setCacheSeconds(int cacheSeconds) {
this.cacheSeconds = cacheSeconds;
}
public int getCacheSeconds() {
return cacheSeconds;
}
public ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
// you now have a Spring Controller wired in, and you could delegate to
// your legacy servlet in this manner
YourServlet servlet = new YourServlet();
servlet.doGet(request, response);
ModelAndView mav = new ModelAndView("hello");
mav.addObject("message", "Hello World!");
return mav;
}
}
<bean id="sampleController" class="samples.SampleController">
<property name="cacheSeconds" value="120"/>
</bean>
Note that manually instantiating a servlet is not really considered a good practice or good design. Instead, this is more of a stepping stone to get your Spring Controllers wired in and connected so that you can then systematically refactor your code and eliminate the legacy servlets by moving your business logic to service layers.
This approach jives with your plan to systematically migrate to and learn more about Spring, tackle some technical debt, all while still working on business goals related to your site.

How to configure Spring 3.0 annotations based xml file?

I'm new to Spring, and searching how to configure it's XML file so all beans will be created by container? And how I can tell in application to load this file on server start-up?
Thanks in advance!
Sample available at https://anonsvn.springframework.org/svn/spring-samples/mvc-basic/. Look at the web.xml and the spring config in .../WEB-INF/spring/appServlet/servlet-context.xml.
You should specify the servlet in web.xml in following way for Spring.
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
also you need to create the xml file called dispatcher-servlet.xml where you can specify the beans you would like the Spring Framework to create for you.
Hope that helps you.
Cheers.
If you're working on a Spring MVC application, follow the indication #Japan Trivedi gave you.
If case you're working on a standalone Spring application, here's an example :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="package.containing.your.bean.classes" />
</beans>
Suppose this configuration file is named "spring-config.xml"
Simply put this file in the classpath of your application and in the code, initialize the Spring application context like this :
ApplicationContext context = new ClasspathXmlApplicationContext("spring-config.xml");
Then, from the context object, you'll be able to retrieve the beans, which have been automatically instantiated by Spring.
Note that this solution does not fully applied IOC concept as you actually explicitly when and what bean you retrieve.

Resources