GWT表单将int传递给doGet()servlet

时间:2019-01-24 19:24:56

标签: java servlets gwt

我使用doGet()方法向Servlet提交了表单。我需要的是通过doGet()将ID传递给Servlet,然后在该方法中对其进行检索。

到目前为止,我尝试了什么:添加一个id作为查询字符串,并在doGet中使用request.getParameter()。我在doPost()及其工作中使用了相同的方法。

客户端代码

downloadPanel = new FormPanel();
downloadPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
downloadPanel.setMethod(FormPanel.METHOD_GET);

downloadPanel.setAction(GWT.getModuleBaseURL()+"downloadfile" + "?entityId="+ 101);
downloadPanel.submit();  

服务器端servlet

public class FileDownload extends HttpServlet {

private String entityId;

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

entityId = request.getParameter("entityId");

entityId为null。如何将ID传递给doGet()请求? 至于在线查看示例,这应该可以直接用于doPost()。谢谢,因为我很沮丧

1 个答案:

答案 0 :(得分:1)

查询参数在操作字段(submitting a GET form with query string params and hidden params disappear)中被忽略。您应该将其添加为隐藏参数(how can i add hidden data on my formPanel in gwt):

FormPanel form = new FormPanel();
form.setEncoding(FormPanel.ENCODING_URLENCODED); // use urlencoded
form.setMethod(FormPanel.METHOD_GET);
FlowPanel fields = new FlowPanel(); // FormPanel only accept one widget
fields.add(new Hidden("entityId", "101")); // add it as hidden
form.setWidget(fields); 
form.setAction(GWT.getModuleBaseURL() + "downloadfile");
form.submit(); // then the browser will add it as query param!

如果您不使用urlencoded,也可以使用request.getParameter(…)使用它,但是它将在正文中而不是URL中传输。