Monday, October 26, 2009

Re: how to send variables in form panel

You have to set the name of the textbox otherwise it won't be addressable on the server side.

Example:

private final FormPanel formPanel = new FormPanel();
private final FileUpload fileUpload = new FileUpload();
private final TextBox textBox = new TextBox();
private final Button uploadButton = new Button("Upload");

textBox.setName("text");

FlexTable flexTable = new FlexTable();
flexTable.setWidget(0, 0, fileUpload);
flexTable.setWidget(1, 0, uploadButton);
flexTable.setWidget(2, 0, textBox);

formPanel.setAction("URL HERE");
formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
formPanel.setMethod(FormPanel.METHOD_POST);
formPanel.setWidget(flexTable);

Now, on the server side using commons-fileupload:

final DiskFileItemFactory factory = new DiskFileItemFactory(maxFileSizeBytes, tempDir);
final ServletFileUpload upload = new ServletFileUpload(factory);

final List<FileItem> items = (List<FileItem>) upload.parseRequest(request);

for (FileItem item : items) {
                if (item.isFormField()) {
                   logger.info(item.getFieldName()+" "+item.getString());
                } else {
                    // process the file bytes
                }
            }

You should see it print your textbox...where item.getFieldName() will return "text"

Regards,
Davis

On Fri, Oct 23, 2009 at 12:19 PM, YoeZ <juzran@gmail.com> wrote:


yes, of course I put the multipart in client code:

    form.setAction(GWT.getModuleBaseURL() + "uploadfileservlet");
    form.setEncoding(FormPanel.ENCODING_MULTIPART);
    form.setMethod(FormPanel.METHOD_POST);

I have a form with uploadfile widget, and some textbox.
the uploaded file was successfully sent to server,, but the textbox
item is null return in servlet.

is there any other way to uploadfile with some field (texboxes)?
instead of using formpanel?



On Oct 23, 12:20 pm, QBox <qbox2...@gmail.com> wrote:
> Sohttp://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/g...
> <http://google-web-toolkit.googlecode.com/svn/javadoc/1.6/com/google/g...>
> Class FormPanel
>
> A panel that wraps its contents in an HTML <FORM> element.
>
> YoeZ say that he use This panel so it will be multipart ;)
>
>
>
> On Thu, Oct 22, 2009 at 8:57 PM, Sudeep S <sudee...@gmail.com> wrote:
> > multipart requests have to be sent via a FORM panel
>
> > On Thu, Oct 22, 2009 at 6:53 PM, Lazo Apostolovski <qbox2...@gmail.com>wrote:
>
> >> Its probably a multipart message :S
>
> >> Try googling for how to accept multipart message on the server side
>
> >> here is some example but i don't know if you find usefull.
>
> >> public HashMap<String, InputStream> parseMultipartMessage(
> >>      HttpServletRequest request) {
>
> >>    mapWithStreams = new HashMap<String, InputStream>();
>
> >>    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
> >>    try {
> >>      if (isMultipart) {
> >>        FileItemFactory factory = new DiskFileItemFactory();
> >>        ServletFileUpload upload = new ServletFileUpload(factory);
>
> >>        List<FileItem> items = upload.parseRequest(request);
>
> >>        for (FileItem item : items) {
> >>          mapWithStreams.put(item.getFieldName(), item.getInputStream());
> >>        }
> >>      } else {
> >>        mapWithStreams.put(DEFAULT_KEY, request.getInputStream());
> >>      }
> >>    } catch (FileUploadException e) {
> >>      e.printStackTrace();
> >>    } catch (IOException e) {
> >>      e.printStackTrace();
> >>    }
> >>    return mapWithStreams;
>
> >> In normal message (not multipart) data from the text fields come like
> >> the body of the message. If you try to transfer file in normal message
> >> then Stream is send like the body of the message. So you can transver
> >> only Parameters or only stream. If you want to transfer parameters and
> >> stream in same message you need to use multipart message. In multipart
> >> message InputStream come on server like a body of the message. If that
> >> input stream contain multipart message you need to parse that message.
> >> So in one part you will have values from input fields and in other part
> >> you will have Stream for uploaded file. Read about multipart messages on
> >> the wiki site. Code abowe will parse all input streams from multipart
> >> message and place them to the HasMap. The Key in HashMap is the File
> >> name. You can access to streams later when you need them. To the
> >> parameters you can access on the same way like you deed before:
>
> >>    request.getParameter("parameterName");
>
> >> Good luck.
>
> >> YoeZ wrote:
> >> > Hey abhiram,
>
> >> > I still have a problem in server side,, can you tell me how to catch
> >> > variable from client.
>
> >> > Let say I have a textbox in client.
>
> >> > Textbox txtComment = new Textbox;
> >> > txtComment.setName("txtComment");
> >> > txtComment .setText("hello");
>
> >> > in server side:
> >> > I've tried:
> >> > String varXXX = (String) request.getParameter("txtComment");
>
> >> > but that's not working. :(
>
> >> > and I've also tried like this :
>
> >> >         try {
> >> >             List items = upload.parseRequest(request);
> >> >             Iterator it = items.iterator();
> >> >             while (it.hasNext()) {
> >> >                 FileItem item = (FileItem) it.next();
> >> >                 if (item.isFormField()) {
> >> >                       String name = item.getFieldName();
> >> >                       if (name.equals("txtComment")) {
> >> >                               ret = item.getString();
> >> >                       }
> >> >                     return ret;
> >> >                 }
> >> >             }
> >> >         } catch (FileUploadException e) {
> >> >             return null;
> >> >         }
>
> >> > but still null return.
>
> >> > can you tell me how to catch it.
>
> >> > regards
>
> >> > On Oct 21, 12:32 am, abhiram wuntakal <abhir...@gmail.com> wrote:
>
> >> >> Hey Yoez,
>
> >> >>   Not sure if it helps. But i have a workaround solution for this. Make
> >> it a
> >> >> two-step process. First use a RemoteServiceServlet to pass across your
> >> >> variables to the server side and then save these values into some
> >> variables
> >> >> on the server side.
>
> >> >>  Then you can use a HTTPServlet to pass across your file contents to
> >> the
> >> >> server side. Now you have the file as well as the variables.
>
> >> >>  HTH,
> >> >> Cheers,
> >> >> Abhiram
>
> >> >> On Mon, Oct 19, 2009 at 6:12 PM, YoeZ <juz...@gmail.com> wrote:
>
> >> >>> thanks ian,, but i'm using GWT and tried to uploadfile which is must
> >> >>> using FormPanel.
> >> >>> I have successfully upload my file to server with UploadFile widget,
> >> >>> but I have another textboxes too. and dunno how to catch inside
> >> >>> FormPanel.
>
> >> >>> On Oct 19, 7:15 pm, Ian Bambury <ianbamb...@gmail.com> wrote:
>
> >> >>>> You'll stand more chance of an answer if you ask that question in a
> >> Java
> >> >>>> forum. This is a GWT group.
> >> >>>> Ian
>
> >> >>>>http://examples.roughian.com
>
> >> >>>> 2009/10/19 YoeZ <juz...@gmail.com>
>
> >> >>>>> hello... please help
>
> >> >>>>> On Oct 18, 12:31 am, YoeZ <juz...@gmail.com> wrote:
>
> >> >>>>>> Hi.
>
> >> >>>>>> I have 1 form panel with some textboxes and file upload.
> >> >>>>>> the question is, how to get variables from client/form panel in
> >> >>>>>> servlet?
> >> >>>>>> i created DTO/Pojo object to hold variables in client, but i donk
>
> >> >>> know
>
> >> >>>>>> how to catch in servlet..
>
> >> >>>>>> please help
>
> >> >>>>>> thanks




--
Zeno Consulting, Inc.
home: http://www.zenoconsulting.biz
blog: http://zenoconsulting.wikidot.com
p: 248.894.4922
f: 313.884.2977

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate