How to process a form submitted by the Google Web Toolkit to a servlet

I followed a tutorial that showed how to submit a form to GAE and submit it to a servlet in the same application. When I submit a form that the servlet responds to, but I cannot read the data using getParameters () or any other method that I was looking for; I do not know if the problem is a client or server, or both. here is the entry point code:

public class HelloWorld implements EntryPoint { public void onModuleLoad() { // Create a FormPanel and point it at a service. final FormPanel form = new FormPanel(); form.setAction("http:// won let me write the host:8888/helloworld/receive"); form.setEncoding(FormPanel.ENCODING_MULTIPART); form.setMethod(FormPanel.METHOD_POST); // Create a panel to hold all of the form widgets. VerticalPanel panel = new VerticalPanel(); panel.setSpacing(10); form.setWidget(panel); // Create a TextBox, giving it a name so that it will be submitted. final TextBox tb = new TextBox(); tb.setWidth("220"); tb.setName("textBoxFormElement"); panel.add(tb); // Create a ListBox, giving it a name and // some values to be associated with its options. ListBox lb = new ListBox(); lb.setName("listBoxFormElement"); lb.addItem("item1", "item1"); lb.addItem("item2", "item2"); lb.addItem("item3", "item3"); lb.setWidth("220"); panel.add(lb); // Create a FileUpload widget. FileUpload upload = new FileUpload(); upload.setName("uploadFormElement"); panel.add(upload); // Add a 'submit' button. panel.add(new Button("Submit", new ClickHandler() { @Override public void onClick(ClickEvent event) { form.submit(); } })); // Add an event handler to the form. form.addSubmitHandler(new FormPanel.SubmitHandler() { @Override public void onSubmit(SubmitEvent event) { // This event is fired just before the form is submitted. // We can take this opportunity to perform validation. if (tb.getText().length() == 0) { Window.alert("The text box must not be empty"); event.cancel(); } } }); form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() { @Override public void onSubmitComplete(SubmitCompleteEvent event) { // When the form submission is successfully completed, // this event is fired. Assuming the service returned // a response of type text/html, we can get the result // here. Window.alert(event.getResults()); } }); DecoratorPanel decoratorPanel = new DecoratorPanel(); decoratorPanel.add(form); // Add the widgets to the root panel. RootPanel.get().add(decoratorPanel); 

}

I tried 20 different things on the servlet to no avail. here is the code

 public class FormHandler extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doPost(req, resp); System.out.println("got to servlet doPost"); System.out.println(req.getParameter("textBoxFormElement")); Enumeration e = req.getParameterNames(); while(e.hasMoreElements()) { String s = e.nextElement().toString(); System.out.println(s); } System.out.println("done for now"); } 

}

this is the result: got to the doPost servlet ....... ............ zero ........ done at the moment

0
java java-ee google-app-engine gwt
source share
1 answer

all your code is right, only one mistake you made. You get encrypted data on the servlet so that you cannot get the value, so you need to write the code as it should, because I already did it in my project.

You need two jar files

  • commons-fileupload-1.2.1.jar

  • commons-io-1.2.jar

     @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try{ ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iterator = upload.getItemIterator(req); while (iterator.hasNext()) { FileItemStream item = iterator.next(); InputStream stream = item.openStream(); if (item.isFormField()) { byte[] str = new byte[stream.available()]; stream.read(str); String pFieldValue = new String(str,"UTF8"); if(item.getFieldName().equals("textBoxFormElement")){ System.out.println("text Value : "+pFieldValue); } } } }catch (Exception e) { e.printStackTrace(); } } 

hope this helps you

+3
source share

All Articles