0

I need help in retrieving the parameters passed when the user types in the url as http://localhost:8182/trace/abc/def?param=123 where the parameter passed is 123. How do i get the 123 displayed on the web browser.Which java classes should i change in order to retrieve and return the parameters in the web page

Here are the codes that i have:

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() ;
    } }

Main

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();  
    }}

Output that i got: Resource URI : http://localhost:8182/trace/abc/def?param=123 Remaining part: /abc/def?param=123

Help is needed urgently!! thanks

1 Answers1

0

I suggest you read the Restlet Documentation and online help

Here's how you can access the parameters within the @Get method of the ServerResource class: (Example borrowed from the documentation link above)

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

You can just return a string representation of the parameter's value (i.e. 123 in your case).

You can get the entire query string (as a String) if you instead say:

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

But you'll have to parse if for the values yourself in that case. The former is better since it does that for you and gives you access to a Parameter List :)

These are explained in the Javadocs

PhD
  • 11,202
  • 14
  • 64
  • 112