How to deploy restlet 2.2 to Tomcat using Servlet

Source: Internet
Author: User

Restlet 2.2 provides Servlet and spring extensions, allowing you to easily deploy restlet to Tomcat and other environments.

This article describes how to use restlet 2.2 servlet extension to deploy Tomcat environment.

1. deployment through org. restlet. Ext. servlet. serverservlet


1. Implement with Application

1) create a class that inherits serverresource

 
Package Org. teamlet. rest. component; import Org. restlet. resource. get; import Org. restlet. resource. serverresource; public class componentresource extends serverresource {@ get Public String represent () {return "Hello, world ";}}

2) create a class that inherits the application

Package Org. teamlet. rest. component; import Org. restlet. application; import Org. restlet. restlet; import Org. restlet. routing. router; public class componentapplication extends application {@ overridepublic synchronized restlet createinboundroot () {router = new router (getcontext (); router. attach ("/Hello", componentresource. class); Return router ;}}

3) modify web. xml

<Context-param> <param-Name> Org. restlet. application </param-Name> <param-value> Org. teamlet. rest. component. componentapplication </param-value> </context-param> <servlet-Name> restletservlet </servlet-Name> <servlet-class> Org. restlet. ext. servlet. serverservlet </servlet-class> </servlet> <servlet-mapping> <servlet-Name> restletservlet </servlet-Name> <URL-pattern>/rest/* </URL -Pattern> </servlet-mapping>

4) after deployment, access through http: // 127.0.0.1: 8080/myservice/rest/Hello

2. Implement with component

1) create two classes that inherit serverresource: the same method as above

2) create two classes that inherit the application: The method is the same as above, and the resource class corresponds to the corresponding application

3) create a class that inherits component: (Note: One and Two can be bound to different applications. I will write the same one here)

 
Package org. teamlet. Rest. component;
 
Public class componentcomponent extends Org. restlet. component {public componentcomponent () {getdefaulthost (). attach ("/one", new componentapplication (); getdefaulthost (). attach ("/Two", new componentapplication ());}}

4) Configure web. xml

 
<Init-param> <param-Name> Org. restlet. component </param-Name> <param-value> Org. teamlet. rest. component. componentcomponent </param-value> </init-param> <servlet-Name> restletservlet </servlet-Name> <servlet-class> Org. restlet. ext. servlet. serverservlet </servlet-class> </servlet> <servlet-mapping> <servlet-Name> restletservlet </servlet-Name> <URL-pattern>/rest/* </URL -Pattern> </servlet-mapping>

5) after deployment, access through http: // 127.0.0.1: 8080/myservice/rest/One and http: // 127.0.0.1: 8080/myservice/rest/Two

2. Use org. restlet. Ext. servlet. servletadapter to deploy

1. Create a servlet class that inherits httpservlet

2. Create a restlet instance trace in the servlet and rewrite the logic in the handle method.

Public class testservlet extends httpservlet {private servletadapter adapter; Public void Init () throws servletexception {super. init (); this. adapter = new servletadapter (getservletcontext (); restlet trace = new restlet (this. adapter. getcontext () {public void handle (request req, response res) {getlogger (). info ("Hello World"); Res. setentity ("Hello world! ", Mediatype. text_plain) ;}}; this. adapter. setnext (TRACE);} protected void Service (httpservletrequest req, httpservletresponse res) throws servletexception, ioexception {This. adapter. service (req, Res );}}

3. Configure web. xml

<Servlet> <servlet-Name> restletservlet </servlet-Name> <servlet-class> Org. teamlet. rest. component. testservlet </servlet-class> </servlet> <servlet-mapping> <servlet-Name> restletservlet </servlet-Name> <URL-pattern>/rest/* </URL -Pattern> </servlet-mapping>

4. This method is usually used in the spring environment. The next article introduces the usage of this servletadapter during spring deployment.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.