servlet不能在GWT中工作 [英] servlet not working in GWT
问题描述
我试图在GWT中使用servlet。并找到
错误。
找不到找到的文件:/ uploadfile / uploadFileServlet
,除非你有任何具体的理由。
$ c
我想浏览一个文件并在服务器端发送它。
我经历了许多专家提供的解决方案。但我无法找到我的错误
有一个plz帮助解决它。
客户端
package uploadfile.client;
public class Uploadfile实现EntryPoint {
@SuppressWarnings(deprecation)
public void onModuleLoad(){
// TODO自动生成的方法存根
final FormPanel uploadForm = new FormPanel();
uploadForm.setAction(GWT.getModuleBaseURL()+uploadFileServlet);
uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
uploadForm.setMethod(FormPanel.METHOD_POST);
//创建一个面板来容纳所有的表单小部件。
VerticalPanel面板=新的VerticalPanel();
uploadForm.setWidget(panel);
//创建一个FileUpload小部件。
FileUpload upload = new FileUpload();
upload.setName(uploadFormElement);
panel.add(上传);
//添加一个'提交'按钮。
Button uploadSubmitButton =新建按钮(提交);
panel.add(uploadSubmitButton);
uploadSubmitButton.addClickListener(new ClickListener(){
public void onClick(Widget sender){$ b $ uploadForm.submit();
}
});
uploadForm.addFormHandler(new FormHandler(){
public void onSubmit(FormSubmitEvent event){
}
public void onSubmitComplete(FormSubmitCompleteEvent event){
Window.alert event.getResults());
}
});
RootPanel.get()。add(uploadForm);
}
}
服务器端
package uploadfile.server;
public class UploadFileServlet extends HttpServlet implements Servlet
{
private static final long serialVersionUID = 8305367618713715640L;
$ b保护无效doGet(HttpServletRequest请求,
HttpServletResponse响应)
抛出ServletException,IOException {
doPost(request,response);
$ b保护无效doPost(HttpServletRequest请求,
HttpServletResponse响应)
抛出ServletException,IOException {
response.setContentType(text /纯);
FileItem uploadItem = getFileItem(request);
if(uploadItem == null){
response.getWriter()。write(NO-SCRIPT-DATA);
return;
}
byte [] fileContents = uploadItem.get();
// TODO:在这里添加代码来处理文件内容。我们将只打印
System.out.println(new String(fileContents));
response.getWriter()。write(new String(fileContents));
私有FileItem getFileItem(HttpServletRequest请求){
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
尝试{
List items = upload.parseRequest(request);
Iterator it = items.iterator(); (it.hasNext()){
FileItem item =(FileItem)it.next();
if(!item.isFormField()
&&uploadFormElement.equals(item.getFieldName())){
return item;
}
}
} catch(FileUploadException e){
return null;
}
返回null;
}
}
web.xml
<?xml version =1.0encoding =UTF-8?>
< web-app version =2.5xmlns =http://java.sun.com/xml/ns/javaee
xmlns: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> fileUploaderServlet< / servlet-name>
< servlet-class> uploadfile.server.UploadFileServlet< / servlet-class>
< / servlet>
<! - Servlets
要服务的默认页面 - >
< servlet-mapping>
< servlet-name> fileUploaderServlet< / servlet-name>
< url-pattern> / uploadFileServlet< / url-pattern>
< / servlet-mapping>
< welcome-file-list>
< welcome-file> Uploadfile.html< / welcome-file>
< / welcome-file-list>
< / web-app>
解决方案我不是GWT开发者,所以它可能会有所不同,我认为你的servlet动作url是错误的。您正在尝试使用路径
/ uploadfile / uploadFileServlet
的servlet,但是您的servlet实际上已映射到url/ uploadFileServlet
。
另外,如果你扩展
Servlet
> HttpServletI am trying to work with servlet in GWT. and i found Error.
No file found for: /uploadfile/uploadFileServlet
i want to browse a file and send it on the server side.
i went through many solution provided by the experts. But i am not able to find my mistake
some one plz help to solve it.
Client Side
package uploadfile.client; public class Uploadfile implements EntryPoint { @SuppressWarnings("deprecation") public void onModuleLoad() { // TODO Auto-generated method stub final FormPanel uploadForm = new FormPanel(); uploadForm.setAction(GWT.getModuleBaseURL() +"uploadFileServlet"); uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART); uploadForm.setMethod(FormPanel.METHOD_POST); // Create a panel to hold all of the form widgets. VerticalPanel panel = new VerticalPanel(); uploadForm.setWidget(panel); // Create a FileUpload widget. FileUpload upload = new FileUpload(); upload.setName("uploadFormElement"); panel.add(upload); // Add a 'submit' button. Button uploadSubmitButton = new Button("Submit"); panel.add(uploadSubmitButton); uploadSubmitButton.addClickListener(new ClickListener() { public void onClick(Widget sender) { uploadForm.submit(); } }); uploadForm.addFormHandler(new FormHandler() { public void onSubmit(FormSubmitEvent event) { } public void onSubmitComplete(FormSubmitCompleteEvent event) { Window.alert(event.getResults()); } }); RootPanel.get().add(uploadForm); } }
Server side
package uploadfile.server; public class UploadFileServlet extends HttpServlet implements Servlet { private static final long serialVersionUID = 8305367618713715640L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/plain"); FileItem uploadItem = getFileItem(request); if (uploadItem == null) { response.getWriter().write("NO-SCRIPT-DATA"); return; } byte[] fileContents = uploadItem.get(); //TODO: add code to process file contents here. We will just print System.out.println(new String(fileContents)); response.getWriter().write(new String(fileContents)); } private FileItem getFileItem(HttpServletRequest request) { FileItemFactory factory = new DiskFileItemFactory(); ServletFileUpload upload = new ServletFileUpload(factory); try { List items = upload.parseRequest(request); Iterator it = items.iterator(); while (it.hasNext()) { FileItem item = (FileItem) it.next(); if (!item.isFormField() && "uploadFormElement".equals(item.getFieldName())) { return item; } } } catch (FileUploadException e) { return null; } return null; } }
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>fileUploaderServlet</servlet-name> <servlet-class>uploadfile.server.UploadFileServlet</servlet-class> </servlet> <!-- Servlets Default page to serve --> <servlet-mapping> <servlet-name>fileUploaderServlet</servlet-name> <url-pattern>/uploadFileServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>Uploadfile.html</welcome-file> </welcome-file-list> </web-app>
解决方案Am not a GWT developer so it could be different, i think your servlet action url is wrong. You are trying for a servlet with path
/uploadfile/uploadFileServlet
but your servlet is actually mapped to the url/uploadFileServlet
.One more thing, there is no need for you to implement
Servlet
if you are extendingHttpServlet
unless you have any specific reasons to do.这篇关于servlet不能在GWT中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!