Restlet2.1 Study Notes (2) process GetPostPut requests separately

Source: Internet
Author: User
Servlet only supports GET and POST requests. In addition to GET and POST requests, restlet also supports DeletePutOPTIONS and other requests. Step 1: Compile the resource class (think of the Resource class as the Struts2 Action, and each method with the annotation is an ActionMethod) MovieResource. javapackagecom. zf. r

Servlet only supports GET and POST requests. In addition to GET and POST requests, restlet also supports Delete Put OPTIONS and other requests. Step 1: Compile the resource class (you can think of the Resource class as the Struts2 Action, and each method with the annotation is an ActionMethod) MovieResource. java package com. zf. r

Servlet only supports GET and POST requests.

In addition to GET and POST requests, restlet also supports Delete Put OPTIONS and other requests.


Step 1: Compile the resource class

(You can think of the Resource class as the Struts2 Action. Each method with the annotation is an ActionMethod)

MovieResource. java

Package com. zf. restlet. demo02.server; import org. restlet. resource. delete; import org. restlet. resource. get; import org. restlet. resource. post; import org. restlet. resource. put; import org. restlet. resource. serverResource;/*** take Method in 3 as an example * @ author zhoufeng **/public class MovieResource extends ServerResource {@ Getpublic String play () {return "the movie is playing... ";}@ Postpublic String pause () {return" movie pause... ";}@ Putpublic String upload () {return" the movie is being uploaded... ";}@ Deletepublic String deleteMovie () {return" deletes a movie... ";}}

Step 2: Use the html client for access (html only supports get and post access by default. So here are two examples)

Demo02.html

 demo02    

You can use two buttons to send different requests and return different values.



Step 3: Use Restlet to write client calls

MovieClient. java

Package com. zf. restlet. demo02.client; import java. io. IOException; import org. junit. test; import org. restlet. representation. representation; import org. restlet. resource. clientResource; public class MovieClient {@ Testpublic void test01 () throws IOException {ClientResource client = new ClientResource (" http://localhost:8888/ "); Representation result = client. get (); // call the get method System. out. println (result. getText () ;}@ Testpublic void test02 () throws IOException {ClientResource client = new ClientResource (" http://localhost:8888/ "); Representation result = client. post (null); // call the post method System. out. println (result. getText () ;}@ Testpublic void test03 () throws IOException {ClientResource client = new ClientResource (" http://localhost:8888/ "); Representation result = client. put (null); // call the put method System. out. println (result. getText () ;}@ Testpublic void test04 () throws IOException {ClientResource client = new ClientResource (" http://localhost:8888/ "); Representation result = client. delete (); // call the delete method System. out. println (result. getText ());}}

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.