0

当用户在 URL 中键入 http://localhost:8182/trace/abc/def?param=123时,我需要帮助来检索传递的参数 ,其中传递的参数是 123。如何让 123 显示在网络上浏览器。为了检索和返回网页中的参数,我应该更改哪些 java 类

这是我拥有的代码:

Part05
import org.restlet.Component;
//import org.restlet.Server;
import org.restlet.data.Protocol;
import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;


public class Part05 extends ServerResource {  

    public static void main(String[] args) throws Exception {  
        // Create the HTTP server and listen on port 8182  
        //new Server(Protocol.HTTP, 8182, Part05.class).start(); 

     // TODO Auto-generated method stub 
        // Create a new Restlet component and add a HTTP server connector to it  
        Component component = new Component();  
        component.getServers().add(Protocol.HTTP, 8182);  

        // Then attach it to the local host  
        component.getDefaultHost().attach("/trace", Part05.class);  

        // Now, let's start the component!  
        // Note that the HTTP server connector is also automatically started.  
        component.start();  
    }  
    @Get  
    public String toString() {  

        // Print the requested URI path  
        return "Resource URI  : " + getReference() + '\n' + "Root URI      : "  
                + getRootRef() + '\n' + "Routed part   : "  
                + getReference().getBaseRef() + '\n' + "Remaining part: "  
                + getReference().getRemainingPart() ;
    } }

主要的

import org.restlet.Component;
import org.restlet.data.Protocol;

public class Main {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception 
    {
        // TODO Auto-generated method stub 
            // Create a new Restlet component and add a HTTP server connector to it  
            Component component = new Component();  
            component.getServers().add(Protocol.HTTP, 8182);  

            // Then attach it to the local host  
            component.getDefaultHost().attach("/trace", Part05.class);  

            // Now, let's start the component!  
            // Note that the HTTP server connector is also automatically started.  
            component.start();  
    }}

我得到的输出:资源 URI:http://localhost:8182/trace/abc/def?param=123 剩余部分:/abc/def?param=123

急需帮助!!谢谢

4

1 回答 1

0

我建议您阅读 Restlet 文档和在线帮助

以下是如何访问类@Get方法中的参数ServerResource:(从上面的文档链接中借用的示例)

Form form = request.getResourceRef().getQueryAsForm();
for (Parameter parameter : form) {
  System.out.print("parameter " + parameter.getName());
  System.out.println("/" + parameter.getValue());
}

您可以只返回参数值的字符串表示形式(即123在您的情况下)。

String如果您改为说,您可以获得整个查询字符串(作为 a ):

request.getResourceRef().getQuery(); // or getQuery(true) if you wish to have URI decoding

但是在这种情况下,您必须自己解析 if 的值。前者更好,因为它可以为您做到这一点并让您可以访问参数列表:)

这些在 Javadocs 中有解释

于 2012-03-13T19:32:34.073 回答