问题描述
我试图在GWT提交FormPanel时调用一个servlet。我在form.setAction(myurl)上设置了url,并且在web.xml上做了映射,但是在表单提交时servlet并不是这里是代码:
pre $ public $ {
$ b $ 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){
//这个事件在表单提交之前被触发。
RootPanel.get()。add(new Label(On submit));
}
public void onSubmitComplete() FormSubmitCompleteEvent event){
//当表单提交成功完成时,这个事件是
//被解雇,假设服务返回了text / html类型的响应,
//我们可以得到这里的结果文本(参见
的FormPanel文档//进一步解释)
RootPanel.get()。add(new Label(On submiting complete));
}
});
panel.add(上传);
panel.add(sub);
RootPanel.get()。add(form);
}
}
这里是web.xml
<?xml version =1.0encoding =UTF-8?>
< web-app version =2.5xmlns =http://java.sun.com/xml/ns/javaeexmlns:xsi =http://www.w3.org/2001/ XMLSchema-instancexsi:schemaLocation =http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd\">
< servlet>
< servlet-name>上传< / 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>上传< / 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 {
$ b $ protected void processRequest(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException {
response.setContentType(text / HTML;字符集= UTF-8\" );
PrintWriter out = response.getWriter();
尝试{
// / * TODO在这里输出您的网页
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 =折叠了desc =HttpServlet方法,点击左边的+号来编辑代码。 >
/ **
*处理HTTP< code> GET< / code>方法。
* @param请求servlet请求
* @param响应servlet响应
* @throws ServletException,如果发生特定于servlet的错误
* @throws IOException,如果发生I / O错误
* /
@Override
保护无效doGet(HttpServletRequest请求,HttpServletResponse响应)
抛出ServletException,IOException {
processRequest(request,response);
}
/ **
*处理HTTP< code> POST< / code>方法。
* @param请求servlet请求
* @param响应servlet响应
* @throws ServletException,如果发生特定于servlet的错误
* @throws IOException,如果发生I / O错误
* /
@Override
protected void doPost(HttpServletRequest请求,HttpServletResponse响应)
抛出ServletException,IOException {
processRequest(request,response);
}
/ **
*返回servlet的简短描述。
* @return包含servlet描述的字符串
* /
@Override
public String getServletInfo(){
return简短描述;
} //< / editor-fold>
$ / code>
请告诉我如何调用NewServlet来提交FormPanel。
或者,您可以使用: form.setAction(NewServlet);
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.
Or as an alternative you could use: form.setAction("NewServlet");
这篇关于在gwt提交formpanel时调用servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!