Академический Документы
Профессиональный Документы
Культура Документы
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 Tomcat1.
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. 1. 2. 3. 4. What do I need?2 The "hello, world" application3 Run in a Servlet container4 Conclusion5
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.07.
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.
*/ @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; } }
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.
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.
Conclusion
We hope you that enjoyed these first steps and encourage you to check the equivalent page in the Java SE edition14 for standalone deployments of the same application. This can also be a convenient way to develop and test your Restlet application before actually deploying it in a Java EE application server.
Notes