简体   繁体   中英

How to get the request parameter in a Session Bean (EJB)?

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"

Use the @QueryParam annotation. That way you don't need to fiddle with request.getParameter() yourself.

Java code

@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>

追加更多变量

href="/resources/showTrail?roy=show&var1=value1&var2=value3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM