Sun正在致力于的建立RESt风格Web服务的规范, 规范如下
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.ProduceMime;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.UriInfo;
/**
*
* @author dongtg
*
*/
@Path("student/{name}")
public class Students {
HashMap<String,Integer> nameage=new HashMap<String,Integer>();
//String name;
int age=20;
public Students()
{
nameage.put("aaa",20);
nameage.put("bbb",30);
}
@GET
@ProduceMime("text/plain")
public String getInfo(@PathParam("name")String name)
{
return "name is "+name+"; age is "+nameage.get(name;
}
import java.io.IOException;
import com.sun.net.httpserver.HttpServer;
import com.sun.ws.rest.api.container.httpserver.HttpServerFactory;
/**
*
* @author dongtg
*
*/
public class Server {
public static void main(String[] args) throws IOException {
HttpServer server = HttpServerFactory.create("http://localhost:9998/");
server.start();
System.out.println("Server running");
System.out.println("Visit: http://localhost:9998/student");
System.out.println("Hit return to stop...");
System.in.read();
System.out.println("Stopping server");
server.stop(0);
System.out.println("Server stopped");
}
}