简体   繁体   English

如何在会话Bean(EJB)中获取请求参数?

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

My button in a HTML page is: 我在HTML页面中的按钮是:

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

My Java web service is: 我的Java Web服务是:

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? 我如何从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" 我想发送一个HttpRequest作为参数来发送更多参数,然后“roy”

Use the @QueryParam annotation. 使用@QueryParam注释。 That way you don't need to fiddle with request.getParameter() yourself. 这样你就不需要自己摆弄request.getParameter()

Java code Java代码

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

HTML 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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