calling servlet on submiting formpanel in gwt

徘徊边缘 提交于 2020-01-01 16:53:28

问题


I am trying to call a servlet when submitting the FormPanel in GWT.I am setting the url on form.setAction("myurl") and also done mapping on the web.xml but while on the form suvmit the servlet is not called.Here is the code:

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

    }

}

Here is the 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>

And NewServlet.java

public class NewServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           // /* TODO output your page here
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet NewServlet</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet NewServlet at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");

        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

Please tell me how can I call the NewServlet on submitting the FormPanel.


回答1:


Or as an alternative you could use: form.setAction("NewServlet");




回答2:


You probably want form.setAction(GWT.getHostPageBaseURL() + "NewServlet")




回答3:


Very similar code, with your exact setAction statement, works for me. Possibly the problem was just that

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

left out the dotted path, e.g. org.fileup.server., to NewServlet.



来源:https://stackoverflow.com/questions/6420851/calling-servlet-on-submiting-formpanel-in-gwt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!