1

My button in a HTML page is:

<a id="showTrail" href="/resources/showTrail?roy=show" target="iframe_a">
  <button>Show Trail</button>
</a>

My Java web service is:

package webServices;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;

/**
 * REST Web Service
 *
 * @author mkuchtiak
 */

@Stateless
@Path("/showTrail")
public class ShowTrail{

    @EJB
    private NameStorageBean nameStorage;

    @GET
    @Produces("text/html")
    public String getXml() {
        //String abc = request.getParameter("roy");
        return "<html><body><h1>Hello "+nameStorage.getName()+"!</h1></body></html>";
    }

    @PUT
    @Consumes("text/plain")
    public void putXml(String content) {
        nameStorage.setName(content);
    }

}


How I can get a request parameter from the HTML? Will the following line work?

String abc = request.getParameter("roy");


I want to send an HttpRequest as parameter to send more parameters then "roy"

4

2 回答 2

4

使用@QueryParam注释。这样你就不需要request.getParameter()自己折腾了。

Java 代码

@GET
@Produces("text/html")
public String getXml(@QueryParam("roy") String roy, 
                     @QueryParam("someInt") int someInt,
                     @QueryParam("orderBy") List<String> orderBy) {

HTML

<a id="showTrail" href="/resources/showTrail?roy=show&someInt=4711&orderBy=age&orderBy=name" target="iframe_a">
  <button>Show Trail</button>
</a>
于 2013-05-26T21:07:43.467 回答
0

附加更多变量,例如

href="/resources/showTrail?roy=show&var1=value1&var2=value3
于 2013-05-26T20:31:12.340 回答