我正在尝试在GWT中提交FormPanel时调用servlet。我在form.setAction(“myurl”)上设置了url,并且还在web.xml上进行了映射,但是在表单上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>
和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>
}
请告诉我如何在提交FormPanel时调用NewServlet。
答案 0 :(得分:8)
您可能需要form.setAction(GWT.getHostPageBaseURL() + "NewServlet")
答案 1 :(得分:4)
或者作为替代方案,您可以使用:form.setAction("NewServlet");
答案 2 :(得分:0)
非常相似的代码,使用您的确切setAction语句,对我有用。可能问题只是那个
<servlet-class>NewServlet</servlet-class>
省略了虚线路径,例如org.fileup.server.
,发送到NewServlet。