在gwt中提交formpanel时调用servlet

在gwt中提交formpanel时调用servlet,gwt,servlets,Gwt,Servlets,在GWT中提交FormPanel时,我试图调用servlet。我正在form.setAction(“myurl”)上设置url,并在web.xml上完成映射,但在form suvmit上未调用servlet。下面是代码: public class MainEntryPoint implements EntryPoint { public void onModuleLoad() { AbsolutePanel panel=new AbsolutePanel(); Fi

在GWT中提交FormPanel时,我试图调用servlet。我正在form.setAction(“myurl”)上设置url,并在web.xml上完成映射,但在form suvmit上未调用servlet。下面是代码:

public class MainEntryPoint implements EntryPoint {

  public void onModuleLoad() {
      AbsolutePanel panel=new AbsolutePanel();
      FileUpload upload = new FileUpload();
      upload.setName("file");
      final FormPanel form = new FormPanel();
      form.setWidget(panel);

 form.setMethod(FormPanel.METHOD_POST);
 form.setEncoding(FormPanel.ENCODING_MULTIPART);

 form.setAction("/NewServlet");

RootPanel.get().add(new Label("path="+GWT.getModuleBaseURL()+"/NewServlet"));

 Button sub=new Button("Submit");
   sub.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                RootPanel.get().add(new Label("In click event submiting form"));
                try{
                form.submit();
                }catch(Exception e){RootPanel.get().add(new Label(e+""));}

       }});

       form.addFormHandler(new FormHandler() {
      public void onSubmit(FormSubmitEvent event) {
        // This event is fired just before the form is submitted. We can take
        // this opportunity to perform validation.
                RootPanel.get().add(new Label("On submit"));
      }

      public void onSubmitComplete(FormSubmitCompleteEvent 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 text here (see the FormPanel documentation for
        // further explanation).
                RootPanel.get().add(new Label("On submiting complete"));
      }
    });



 panel.add(upload);
 panel.add(sub);

 RootPanel.get().add(form);

    }

}
这是web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>upload</servlet-name>
        <servlet-class>org.fileup.client.UploadServlet2</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>NewServlet</servlet-name>
        <servlet-class>NewServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>upload</servlet-name>
        <url-pattern>/uploadservlet2</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>NewServlet</servlet-name>
        <url-pattern>/NewServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>welcomeGWT.html</welcome-file>
    </welcome-file-list>
</web-app>

请告诉我在提交FormPanel时如何调用NewServlet。

您可能需要
form.setAction(GWT.getHostPageBaseURL()+“NewServlet”)
或者您可以使用以下替代方法:
form.setAction(“NewServlet”)

非常相似的代码,以及您精确的setAction语句,适用于我。可能问题就在于

<servlet-class>NewServlet</servlet-class>
NewServlet

省去了NewServlet的虚线路径,例如,
org.fileup.server.

在NewServletNo之前需要一个斜杠,
GWT.getHostPageBaseURL()
保证以斜杠结尾。证明:(同样适用于
GWT.getModuleBaseURL()
,顺便说一句)
<servlet-class>NewServlet</servlet-class>