restlet2.0.3第一步

http://www.restlet.org/   官网

 

JavaEE应用

所需jar 包:org.restlet.ext.servlet.jar  ; org.restlet.jar


第一步:新建类,拦捷请求路径:

FirstStepsApplication.java

 

package firstSteps;

import firstSteps.HelloWorldResource;
import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;

/**
 * Created by IntelliJ IDEA.
 * User: liuwen
 * Date: 2010-11-13
 * Time: 13:54:44
 * To change this template use File | Settings | File Templates.
 */
public class FirstStepsApplication extends Application {
     /**
     * Creates a root Restlet that will receive all incoming calls.
     */
    @Override
    public synchronized Restlet createInboundRoot() {
        // Create a router Restlet that routes each call to a new instance of HelloWorldResource.
        Router router = new Router(getContext());

        // Defines only one route
        router.attach("/hello", HelloWorldResource.class);

        return router;
    }
}

 

 

 

第二步:建一个处理相应请求的类,用注解的方式,注明,分别处理:如 get, post ,put ,delete的方法

 

 

package firstSteps;

import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

/**
 * Created by IntelliJ IDEA.
 * User: liuwen
 * Date: 2010-11-13
 * Time: 13:53:53
 * To change this template use File | Settings | File Templates.
 */
public class HelloWorldResource extends ServerResource {


    @Get
    public String represent() {
        return "hello, world";
    }

    
}

 

第三步:在web.xml配置servlet 将   项目下的所有  /restlet/*路径交给 restlet处理,支持项目中以前的方式

web.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">

    <display-name>first steps servlet</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>firstSteps.FirstStepsApplication</param-value>
      </init-param>
   </servlet>

   <!-- Catch all requests -->
   <servlet-mapping>
      <servlet-name>RestletServlet</servlet-name>
      <url-pattern>/restlet/*</url-pattern>
   </servlet-mapping>  


</web-app>

 

 

你可能感兴趣的:(Web,xml,servlet,javaee,ext)