本文主要是介绍Restlet edition for Java EE FirstStepsServlet,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使有方法
一、该项目部署到Tomcat服务器下,启动Tomcat服务器
二、在浏览器地址栏中输入:http://localhost:8080/firstStepsServlet/restlet/hello
输出:成功
方法二:
一、不要开启服务器,直接启动类Run
二、在浏览器地址栏中输入:http://localhost:8182/firstSteps/hello
OK============================================================
更多信息:
http://wiki.restlet.org/docs_2.0/13-restlet/275-restlet/312-restlet.html
Restlet edition for Java EE
Introduction
This chapter presents the Restlet Framework edition for Java EE (Java Enterprise Edition).
This edition is aimed for development and deployment of Restlet applications inside Java EE application server, or more precisely inside Servlet containers such as Apache Tomcat .
Getting started
The rest of this page should get you started with the Restlet Framework, Java EE edition, in less than 10 minutes. It explains how to create a resource that says "hello, world" and run it.
- What do I need?
- The "hello, world" application
- Run in a Servlet container
- Conclusion
What do I need?
We assume that you have a development environment set up and operational, and that you already have installed the Java 1.5 (or higher). In case you haven't downloaded the Restlet Framework yet, select one of the available distributions of the Restlet Framework 2.0 .
The "hello, world" application
Let's start with the core of a REST application: the Resource. Here is the code of the single resource defined by the sample application. Copy/paste the code in your "HelloWorldResource" class.
package firstSteps;import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;/*** Resource which has only one representation.*/
public class HelloWorldResource extends ServerResource {@Getpublic String represent() {return "hello, world";}}
Then, create the sample application. Let's call it "FirstStepsApplication" and copy/paste the following code:
package firstSteps;import org.restlet.Application;
import org.restlet.Restlet;
import org.restlet.routing.Router;public class FirstStepsApplication extends Application {/*** Creates a root Restlet that will receive all incoming calls.*/@Overridepublic 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 routerouter.attach("/hello", HelloWorldResource.class);return router;}}
Run in a Servlet container
Let's now deploy this Restlet application inside your favorite Servlet container. Create a new Servlet Web application as usual, add a "firstStepsServlet" package and put the resource and application classes in. Add the archives listed below into the directory of librairies (/WEB-INF/lib):
- org.restlet.jar
- org.restlet.ext.servlet.jar
Then, update the "web.xml" configuration file 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 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>/*</url-pattern> </servlet-mapping>
</web-app>
Finally, package the whole as a WAR file called for example "firstStepsServlet.war" and deploy it inside your Servlet container. Once you have launched the Servlet container, open your favorite web browser, and enter the following URL:
http://<your server name>:<its port number>/firstStepsServlet/hello
The server will happily welcome you with the expected "hello, world" message. You can find the WAR file (packaged with archives taken from Restlet Framework 2.0 Milestone 5) in the "First steps application" files .
Run as a standalone Java application
A Restlet application cannot only run inside a Servlet container, but can also be run as a standalone Java application using a single "org.restlet.jar" JAR.
Create also a main class, copy/paste the following code wich aims at defining a new HTTP server listening on port 8182 and delegating all requests to the "FirstStepsApplication".
public static void main(String[] args) throws Exception { // Create a new Component. Component component = new Component(); // Add a new HTTP server listening on port 8182. component.getServers().add(Protocol.HTTP, 8182); // Attach the sample application. component.getDefaultHost().attach("/firstSteps", new FirstStepsApplication()); // Start the component. component.start();
}
Once you have launched the main class, if you can open your favorite web browser, and gently type the following URL: http://localhost:8182/firstSteps/hello, the server will happily welcome you with a nice "hello, world". Otherwise, make sure that the classpath is correct and that no other program is currently using the port 8182.
You can find the sources of this sample application in the "First steps application" files .
这篇关于Restlet edition for Java EE FirstStepsServlet的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!