receive data from Formpanel in gwt

2019-08-10 07:17发布

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.

3条回答
老娘就宠你
2楼-- · 2019-08-10 07:20

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.

查看更多
Emotional °昔
3楼-- · 2019-08-10 07:27

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

查看更多
三岁会撩人
4楼-- · 2019-08-10 07:29

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.
查看更多
登录 后发表回答