问题
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.
回答1:
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.
回答2:
Several things here:
- Why don't you use GWT-RPC to communicate with the server? This is the preferred way to transfer data.
- 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 byrequest.getParameter("mycheck")
. - I have never used ServletFileUpload but I believe that it will only provide you the different File parts of your request.
回答3:
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
来源:https://stackoverflow.com/questions/9634001/receive-data-from-formpanel-in-gwt