Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the asciidoc in the repository:

Servlet Tomcat No Spring Example

Available as of Camel 2.11

This example is located in the examples/camel-example-servlet-tomcat-no-spring directory of the Camel distribution.
There is a README.txt file with instructions how to run it.

If you use maven then you can easily package the example from the command line:

mvn package


This example demonstrates how you can create light-weight web applications without the need for Spring Framework.
The example contains a Camel routes that used the Servlet component to expose a http service.


In the web.xml file in the src/main/webapp/WEB-INF folder we have both a CamelServlet and CamelContextListener defined.
The CamelServlet is mandatory to do when using the Servlet component. And the JndiCamelServletContextListener is used to bootstrap the Camel application.

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<web-app version="2.4" xmlns=""

  <display-name>My Web Application</display-name>

  <!-- you can configure any of the properties on CamelContext, eg setName will be configured as below -->

  <!-- location of Camel route xml files -->
    <!-- define the routes as a resource from the classpath by prefixing the value with classpath: -->
    <!-- note: instead of using a XML file we can also define the routes in Java code in a RouteBuilder class -->
    <!-- define a lifecycle which could help user to setup the registry which could be use as a reference in camel route -->
    <!-- If you use org.apache.camel.component.servletlistener.JndiCamelServletContextListener then the CamelContextLifecycle
     must use the JndiRegistry as well. And likewise if the servlet is org.apache.camel.component.servletlistener.SimpleCamelServletContextListener 
     then the CamelContextLifecycle must use the SimpleRegistry -->

  <!-- the listener that kick-starts Camel -->

  <!-- Camel servlet used in the Camel application -->

  <!-- Camel servlet mapping -->


Its the ServletListener Component that is used to bootstrap Camel in the web application. See more details at ServletListener Component.

The Camel route

The route is a simple Content Based Router defined in the DSL XML as shown:

Error rendering macro 'code': Invalid value specified for parameter 'java.lang.NullPointerException'
<!-- here we have the Camel route(s). -->
<!-- we must still use the namespace so Camel can load the routes
     though Spring JARs is not required -->
<routes xmlns="">

    <!-- incoming requests from the servlet is routed -->
    <from uri="servlet:hello"/>
        <!-- is there a header with the key name? -->
        <!-- yes so return back a message to the user -->
        <to uri="bean:myBean"/>
        <!-- if no name parameter then output a syntax to the user -->
          <constant>Add a name parameter to uri, eg ?name=foo</constant>


Running the example

This example runs in any web container such as Apache Tomcat. For example to deploy in Apache Tomcat you will have to package the .war file and copy it to the webapp folder of Tomcat, which is the hot deploy folder.

There is a main page at: http://localhost:8080/camel-example-servlet-tomcat-no-spring which has more instructions.
You can then use a web browser and send a request to the http://localhost:8080/camel-example-servlet-tomcat-no-spring/camel/hello url.

See Also

© 2004-2015 The Apache Software Foundation.
Apache Camel, Camel, Apache, the Apache feather logo, and the Apache Camel project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
Graphic Design By Hiram