1

I am trying to use FormPanel. oN FormPanel

  formPanel.setWidget(flexTable);

A check box , a listBox and FileUpload is added

flexTable.setWidget(4, 1,listBox);
flexTable.setWidget(5, 1, fileUpload);
flexTable.setWidget(6, 1, checkBox);
 // More Code

A Servlet code is written to get the all the values which running fine only for fileUpload. How to get the value of checkBox an ListBox.

protected void doPost(HttpServletRequest request, 
        HttpServletResponse response) 
                        throws ServletException, IOException { 

    byte[] buffer = new byte[1310720];// 10 MB

     try {

        ServletFileUpload upload = new ServletFileUpload();

        FileItemIterator iterator = upload.getItemIterator(request);

         while (iterator.hasNext()) {
             FileItemStream item = iterator.next();
             InputStream stream = item.openStream();

             if (item.isFormField()) {
                  // WHAT TO DO??

                } else {

                  int len;

                  while ((len = stream.read(buffer, 0, buffer.length)) != -1) {
                    response.getOutputStream().write(buffer, 0, len);                       
                  }
                }
         }

    } catch (FileUploadException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Plz help to get the value of checkBox and List Box.

GameBuilder
  • 1,169
  • 4
  • 31
  • 62

3 Answers3

5

See the answer to this question: Passing parameters along with a multipart/form-data upload form (Java Http Post Upload) on how to get the values on the server side.

To send the values to the server you need to set a name on each widget via the setName() method on the ListBox and CheckBox widgets. The name is what item.getFieldName() returns.

Community
  • 1
  • 1
Hilbrand Bouwkamp
  • 13,509
  • 1
  • 45
  • 52
  • @HibrandBouwkamp I have Label on the Formpanel. there is no method `lablel.setName().` **what to do.** – NewCodeLearner Mar 09 '12 at 14:05
  • 1
    @NewCodeLearner Each field posted in a form must be of HTML tag `input`. Label is a `div` so it won't work. As Label is static it doesn't react to user input, so you can use a hidden input field and store the label value in that field. In GWT you can use the `Hidden` widget for it. It doesn't show in the UI, but you should set values to it, when you set the value on Label and that value will be submitten with under the name you set to the hidden widget. – Hilbrand Bouwkamp Mar 09 '12 at 14:20
2

Several things here:

  1. Why don't you use GWT-RPC to communicate with the server? This is the preferred way to transfer data.
  2. If still you do want to use a servlet to handle the request, how do you submit your values? Are your widgets embedded in a Form or do you encode manually their values in a GET-url? If you use a form, then you should add names on your widget element (checkbox.getElement().setAttribute("name", "mycheck");) and in your servlet you get the value by request.getParameter("mycheck").
  3. I have never used ServletFileUpload but I believe that it will only provide you the different File parts of your request.
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117
  • 2
    Except the only way to upload a file is to use a form...It can't be done via GWT-RPC. And instead of using the `setAttribute` way simply use the method `setName` on the widget. – Hilbrand Bouwkamp Mar 09 '12 at 13:03
  • For a file upload, agreed. But to transfer data, the GWT-RPC is the way to go. If he needs to have a single post, then he should include his widgets in the form around the fileupload and set a name on all of them. – Guillaume Polet Mar 09 '12 at 13:13
  • Yes agreed too. If you don't do a FileUpload don't use a form post. – Hilbrand Bouwkamp Mar 09 '12 at 13:16
  • 1
    All the values of the other widget are related to document.so i have put them on formPanel.Thanks I got My answer. `if (item.isFormField()) { String name = item.getFieldName(); String value = Streams.asString(item.openStream()); } else ` – NewCodeLearner Mar 09 '12 at 13:28
2

In What to do of your code.

  String name  = item.getFieldName();
                  String value = Streams.asString(item.openStream());

and dont forget to setName of each widget on the Formpanel

NewCodeLearner
  • 738
  • 3
  • 14
  • 38