Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Wiki Markup
h2. Servlet Component
*Available as of Camel 2.0*

The *servlet:* component provides HTTP based [endpoints|Endpoint] for consuming HTTP requests that arrive at a HTTP endpoint and this endpoint is bound to a published Servlet.

Maven users will need to add the following dependency to their {{pom.xml}} for this component:
{code:xml}
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-servlet</artifactId>
    <version>x.x.x</version>
    <\!-\- use the same version as your Camel core version \-->
</dependency>
{code}

h3. URI format

{code}
servlet://relative_path[?options]
{code}

You can append query options to the URI in the following format, {{?option=value&option=value&...}}

h3. Options
{div:class=confluenceTableSmall}
|| Name || Default Value || Description ||
| {{httpBindingRef}} | {{null}} | Reference to an {{org.apache.camel.component.http.HttpBinding}} in the [Registry]. A {{HttpBinding}} implementation can be used to customize how to write a response. |
| {{matchOnUriPrefix}} | {{false}} | Whether or not the {{CamelServlet}} should try to find a target consumer by matching the URI prefix, if no exact match is found. |
| {{servletName}} | {{null}} | Specifies the servlet name that the servlet endpoint will bind to. If there is no servlet name specified, the servlet endpoint will be bind to first published Servlet |
{div}

h3. Message Headers
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 123. 

h3. Usage

You can only consume from endpoints generated by the Servlet component. Therefore, it should only be used as input into your camel routes. To issue HTTP requests against other HTTP endpoints, use the [HTTP Component|HTTP]

h3. Sample
In this sample, we define a route that exposes a HTTP service at {{http://localhost:8080/camel/services/hello}}.
First, you need to publish the [CamelHttpTransportServlet|http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java] through the normal Web Container, or OSGi Service.
Use the {{Web.xml}} file to publish the [CamelHttpTransportServlet|http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java] as follows:
{snippet:id=web|lang=xml|url=camel/trunk/components/camel-servlet/src/test/resources/org/apache/camel/component/servlet/web.xml}

Then you can define your route as follows:
{snippet:id=route|lang=java|url=camel/trunk/components/camel-servlet/src/test/java/org/apache/camel/component/servlet/HttpClientRouteTest.java}

{note:title=Specify the relative path for camel-servlet endpoint}
Since we are binding the Http transport with a published servlet, and we don't know the servlet's application context path, the {{camel-servlet}} endpoint uses the relative path to specify the endpoint's URL. A client can access the {{camel-servlet}} endpoint through the servlet publish address: {{("http://localhost:8080/camel/services") + RELATIVE_PATH("/hello")}}.{note}

h4. Sample when using Spring 2.x

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. This can be accomplished by using Spring's {{ContextLoaderServlet}} instead of {{ContextLoaderListener}}. In that case you'll need to start {{ContextLoaderServlet}} after [CamelHttpTransportServlet|http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java] like this:

{code:xml} 
<web-app>
  <servlet> 
        <servlet-name>CamelServlet</servlet-name> 
        <servlet-class> 
            org.apache.camel.component.servlet.CamelHttpTransportServlet 
        </servlet-class> 
        <load-on-startup>1</load-on-startup> 
  </servlet> 
  <servlet> 
        <servlet-name>SpringApplicationContext</servlet-name> 
        <servlet-class> 
          org.springframework.web.context.ContextLoaderServlet 
        </servlet-class> 
        <load-on-startup>2</load-on-startup> 
  </servlet> 
<web-app>
{code}

h4. Sample when using OSGi

From *Camel 2.6.0*, you can publish the [CamelHttpTransportServlet|http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java] as an OSGi service with help of SpringDM like this.
{snippet:id=service|lang=xml|url=camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/servlet/ServletServiceContext.xml}

Then use this service in your camel route like this:
{snippet:id=camelContext|lang=xml|url=camel/trunk/tests/camel-itest-osgi/src/test/resources/org/apache/camel/itest/osgi/servlet/CamelServletWithServletServiceContext.xml}

Alternatively - pre Camel 2.6 - you can use an {{Activator}} to publish the [CamelHttpTransportServlet|http://svn.apache.org/repos/asf/camel/trunk/components/camel-servlet/src/main/java/org/apache/camel/component/servlet/CamelHttpTransportServlet.java] on the OSGi platform 
{snippet:id=activator|lang=java|url=camel/trunk/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/servlet/support/ServletActivator.java}

{include:Endpoint See Also}
- [Http]