【问题标题】:How to process a form sent Google Web Toolkit in a servlet如何在 servlet 中处理发送的 Google Web Toolkit 表单
【发布时间】:2013-06-18 11:25:47
【问题描述】:

我按照教程展示了如何在 GAE 中提交表单并将其定向到同一应用程序中的 servlet。当我提交表单时,servlet 会响应,但我无法使用 getParameters() 或我搜索的任何其他方法读取数据;我不知道问题是客户端还是服务器或两者兼而有之。这是入口点代码:

 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);

}

我在 servlet 上尝试了 20 种不同的方法,但均无济于事。这是代码

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");
}

}

这是输出: 到 servlet doPost....... ............无效的 ........暂时完成

【问题讨论】:

    标签: java google-app-engine jakarta-ee gwt


    【解决方案1】:

    您的所有代码都是正确的,只有一个错误。在 servlet 上,您获得了加密数据,因此您无法获得价值,这就是为什么您需要编写如下代码,因为我已经在我的项目中完成了.

    你需要两个jar文件

    1. commons-fileupload-1.2.1.jar

    2. 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();
      }
      }
      

    希望对你有帮助

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-24
    • 1970-01-01
    • 2018-10-15
    • 2015-10-06
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    相关资源
    最近更新 更多