0

I have a form submission to a servlet with a doGet() method. What I need is to pass an id to the servlet via doGet() and retrieve it in that method.

What I tried so far: adding an id as a query string and using request.getParameter() in the doGet. I am using this same method in doPost() and its working.

client side code

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

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

server side servlet

public class FileDownload extends HttpServlet {

private String entityId;

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

entityId = request.getParameter("entityId");

entityId is null. How can i pass an Id to the doGet() request? As for looking at examples online this should work as it works for the doPost() as is. Thanks as i am stumped

1 Answers1

0

Query parameters are ignored in the action field (submitting a GET form with query string params and hidden params disappear). You should add it as a hidden parameter (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!

If you do not use urlencoded it will be available using the request.getParameter(…) too, but it will be transferred in the body instead of the URL.

Ignacio Baca
  • 1,538
  • 14
  • 18
  • This works great. Thanks for taking the time to help me. Its very appreciated. I never seen this come up in any of my searches. You nailed it. Thanks –  Jan 25 '19 at 14:29
  • This solution causes a problem. "com.google.gwt.event.shared.UmbrellaException: Exception caught: SimplePanel can only contain one child widget" I have a drop down and when i select an item i pass the id as hidden. So, this form gets called everytime i make a new selection. I commented out the hidden and no error. Is there a way to clear it and then re add it? –  Jan 25 '19 at 14:48
  • What i ended up doing is creating a new hidden then attaching it so then after the form is sent i remove it. Seems to be working now. –  Jan 25 '19 at 15:43
  • Oh, yes, you need to add acontainer panel, for example a FlowPanel. I'll update the example later. – Ignacio Baca Jan 25 '19 at 16:19