My new website on Artificial Intelligence and Machine Learning

Apache Camel with Servlet Component

The servlet: component provides HTTP based endpoints for consuming HTTP requests that arrive at a HTTP endpoint that is bound to a published Servlet.
Camel will apply the same Message Headers as the HTTP component. Camel will also populate all request.parameter and request.headers. For example, if a client request has the URL, http://myserver/myserver?orderid=123, the exchange will contain a header named orderid with the value 12

You can consume only from endpoints generated by the Servlet component. Therefore, it should be used only as input into your Camel routes. To issue HTTP requests against other HTTP endpoints, use the HTTP Component..First, you need to publish the CamelHttpTransportServlet through the normal Web Container.Use the Web.xml file to publish the CamelHttpTransportServlet as follows.
When using the Servlet component in a Camel/Spring application it's often required to load the Spring ApplicationContext after the Servlet component has started.his can be accomplished by using Spring's ContextLoaderListener.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	id="WebApp_ID" version="2.5">


	<!-- location of spring xml files -->

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

	<!-- Camel servlet -->

	<!-- Camel servlet mapping -->
# The logging properties
log4j.rootLogger=INFO, out

# CONSOLE appender not used by default
log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n

Router(applicationContext-camel.xml):The route is a simple Content Based Router defined in the DSL XML as shown:
<beans xmlns=""
	xsi:schemaLocation="   ">

	<camelContext xmlns="">

    <route id="helloRoute">
      <!-- incoming requests from the servlet is routed -->
      <from uri="servlet:///TestServlet"/>
          <!-- is there a header with the key name? -->
          <!-- yes so return back a message to the user -->
            <simple>Hello ${} how are you?</simple>
          <!-- if no name parameter then output a syntax to the user -->
            <constant>Add a name parameter to uri, eg ?name=foo</constant>



Maven entries for pom.xml


This example runs in Apache Tomcat, so you will have to package the .war file and copy it to the webapp folder of Tomcat. You can then use a web browser and send a request to the http://localhost:8080/ServletProj/camel/TestServlet?name=javavillage

Apache Camel with Servlet Component Application Structure

Apache Camel with Servlet App Structure

Execute Apache Camel with Servlet Component

Execution Apache Camel with Servlet

Still if you face any issues with execution add below jars. Generally while starting tomcat need to add jars in Deployment Assembly. If you don't have option for deployment Assembly add jars in class path of Launch Configuration like below.

Apache Camel with Servlet Launch Configuration