Deploy a servlet based web service on a Tomcat server

In this tutorial, we will see how to deploy a simple RESTful web service on a Tomcat server instead of runing it as a standalone application.

  1. Create a new Dynamic Web Project, named “FirstREST”: File → New → Other → Dynamic Web Project, name it, select the appropriate Tomcat in the “Target runtime”. In my case, I select Tomcat v9.0 and Dynamic web module version 4.0. Then, click Next→Next, then select web.xml generation. Finish.





  2. In the created project, there is a folder call WebContent. Copy the org.restlet.jar and org.restlet.ext.servlet files to the WebContent\WEB-INF\lib folder.
  3. Create the “fifthRest” (check that is fifth nit FIFTH)package under the src folder and copy three files: “RouterApplication”, “UserResource” and “UserItemResource” created in the previous tutorial to this package. Remember to check that there is no error in the project.
  4. Update the “web.xml” file in the WebContent\WEB-INF folder as follow.

    <?xml version="1.0" encoding="UTF-8"?>  
    <web-app id="WebApp_ID" 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">  
       <display-name>first REST</display-name>  
     
       <!-- Restlet adapter -->  
       <servlet>  
          <servlet-name>RestletServlet</servlet-name>  
          <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
          <init-param>
                <!-- Application class name -->
                <param-name>org.restlet.application</param-name>
                <param-value>fifthRest.RouterApplication</param-value>
          </init-param>
       </servlet>  
     
       <!-- Catch all requests -->  
       <servlet-mapping>  
          <servlet-name>RestletServlet</servlet-name>  
          <url-pattern>/*</url-pattern>  
       </servlet-mapping>  
    </web-app>
  5. Export the project to a war file, named “FirstREST.war”: File → Export → WAR file, name it and Finish.
  6. Publish the FirstREST.war file to your Tomcat web server. A simple way is copying it to [your tomcat]/webapps directory.
  7. (Re)start your Tomcat and navigate: http://[server address]:[port]/FirstREST/users/[uid]/items, for example http://localhost:8080/FirstREST/users/123/items.
  8. You can also test the application by runing it on the Tomcat runtime environment set up in Eclipse by selecting the project, then Run → Run As → Run on server or Alt+Shift+X,R.
  9. Done.