[英]Servlet not found from a GWT web application
I am struggling since few days with a problem concerning my GWT application and the servlets called from the application. 几天以来,我一直在努力解决与我的GWT应用程序和从应用程序调用的servlet有关的问题。
I am actually coding a web app with Java and GWT for my research institute. 我实际上是为我的研究所使用Java和GWT编写一个Web应用程序。 The goal is to produce an XML file containing metadata from user inputs, after flight campaigns.
目标是在飞行活动后生成一个包含来自用户输入的元数据的XML文件。 Until then, I always coded my apps with Python or Matlab, and I started to use Java and GWT two months ago (to allow users to access those tools online).
在那之前,我始终使用Python或Matlab编写应用程序代码,并且两个月前开始使用Java和GWT(允许用户在线访问这些工具)。
The web app consists of a client side (the GUI) and a server side with servlets to allow the user to download the generated XML file, to print a pdf report, or to upload the XML file (to work on it again). 该Web应用程序由一个客户端(GUI)和一个带有servlet的服务器端组成,以允许用户下载生成的XML文件,打印pdf报告或上载XML文件(以对其进行再次处理)。 Not really complex.
并不复杂。
Using Eclipse 4.4 and the embedded Jetty server, no problem at all ! 使用Eclipse 4.4和嵌入式Jetty服务器,完全没有问题! It's really nice, the XML file is well formated, the download and upload functions are ok.
真的很好,XML文件格式正确,下载和上传功能还可以。 Then I wanted to test it on my laptop with a Tomcat server (Apache Tomcat 7, for Ubuntu 14.04), I deployed the .war (which was successfully unzipped by Tomcat), and I managed to access the web app from firefox.
然后,我想在装有Tomcat服务器(适用于Ubuntu 14.04的Apache Tomcat 7)的便携式计算机上对其进行测试,并部署了.war(已成功由Tomcat解压缩),并设法从firefox访问了Web应用程序。 But when I try to access the servlets (to upload an xml file), the anwser returned is "HTTP ERROR 404. Problem accessing /upload. Reason: NOT_FOUND" I tried to found a solution using google, and I found few posts on the same subject, but all posted solutions didn't work in my case ...
但是,当我尝试访问servlet(上传xml文件)时,返回的anwser是“ HTTP错误404。访问/ upload时出错。原因:NOT_FOUND”我试图使用google找到解决方案,并且在同一主题,但所有发布的解决方案在我的情况下均不起作用...
Here is the part of my main code to call the servlet: 这是我的主要代码中调用servlet的部分:
final FileUpload myFileUpload = new FileUpload();
myFileUpload.setName("uploadFormElement");
myFileUpload.getElement().setId("uploadFormElement");
myFileUpload.getElement().setId("myFile");
panel.add(myFileUpload);
final FormPanel myUploadForm = new FormPanel();
myUploadForm.setAction("/upload");
myUploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
myUploadForm.setMethod(FormPanel.METHOD_POST);
Here is my servlet code: 这是我的servlet代码:
package com.eufar.asmm.server;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@SuppressWarnings("hiding")
public class UploadFunction<FileItem> extends HttpServlet implements Servlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
@SuppressWarnings("unused")
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try {
List items = upload.parseRequest(request);
Iterator iter = items.iterator();
while(iter.hasNext()){
Object obj = iter.next();
if(obj == null) continue;
org.apache.commons.fileupload.FileItem item = (org.apache.commons.fileupload.FileItem)obj;
if(item.isFormField()){
String name = item.getName();
String value = "";
if(name.compareTo("textBoxFormElement")==0){
value = item.getString();
}
else {
value = item.getString();
}
response.getWriter().write(name + "=" + value + "\n");
}
else {
byte[] fileContents = item.get();
String message = new String(fileContents);
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
response.getWriter().write(message);
}
}
} catch (Exception ex) {
response.getWriter().write("ERROR:" + ex.getMessage());
}
}
}
And here is my web.xml: 这是我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee">
<!-- Servlets -->
<servlet>
<servlet-name>DownloadFunction</servlet-name>
<servlet-class>com.eufar.asmm.server.DownloadFunction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DownloadFunction</servlet-name>
<url-pattern>DownloadFunction</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UploadFunction</servlet-name>
<servlet-class>com.eufar.asmm.server.UploadFunction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadFunction</servlet-name>
<url-pattern>/upload</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>Asmm_eufar.html</welcome-file>
</welcome-file-list>
</web-app>
For information, all seems to be ok in the WEB-INF directory with all classes, all needed libraries ... I did a quick test with Eclipse : to generate the defaut GWT web app ("Enter your name") and deploy it on Tomcat. 有关信息,所有类,所有需要的库在WEB-INF目录中似乎都没问题……我对Eclipse进行了快速测试:生成defaut GWT Web应用程序(“输入您的名字”)并将其部署在Tomcat的。 With Eclipse, no issue, but Tomcat can't find the default servlet ...
使用Eclipse没问题,但是Tomcat找不到默认的servlet ...
So, finally, I can't say if my code is absolutely ok (I am a beginner), and I can't say if the problem can a wrong Tomcat configuration (I am a researcher not server admin). 所以,最后,我不能说我的代码是否完全正确(我是初学者),也不能说问题是否可以是错误的Tomcat配置(我是研究人员而不是服务器管理员)。
I hope you will be able to help me. 希望您能对我有所帮助。
Kind regards, 亲切的问候,
Olivier 奥利维尔
Edit: I probably forgot to add some details about how I added my app to Tomcat and how I launch it. 编辑:我可能忘了添加一些有关如何将应用程序添加到Tomcat以及如何启动它的详细信息。
With Eclipse, I compile the code, then I move all files and directories included in the "war" directory to a directory called "MyApp" (an example). 使用Eclipse,我编译代码,然后将“ war”目录中包含的所有文件和目录移动到名为“ MyApp”的目录(示例)。 Then I compress this directory to a .zip file and changed ".zip" to ".war".
然后,我将该目录压缩为.zip文件,并将“ .zip”更改为“ .war”。 This .war file is then copy/past to the "webapps" directory of Tomcat.
然后,将此.war文件复制/粘贴到Tomcat的“ webapps”目录中。 Tomcat process the .war file and I obtain a directory calle "MyApp", in which I have another "MyApp" directory containing all my files.
Tomcat处理.war文件,然后获得一个名为“ MyApp”的目录,在该目录中还有一个包含所有文件的“ MyApp”目录。 Finally, I launch Firefox and access my app with the following Url : http://localhost:8080/MyApp/MyApp/Myapp.html
最后,我启动Firefox并使用以下网址访问我的应用程序: http:// localhost:8080 / MyApp / MyApp / Myapp.html
With the proposals given by Thomas, I should access the Upload function with the Url http://localhost:8080/MyApp/MyApp/upload . 根据Thomas的建议,我应该使用URL http:// localhost:8080 / MyApp / MyApp / upload访问Upload函数。 But the returned error is: "State HTTP 404. MyApp/MyApp/upload The requested resource is not available (in french on my computer). It differs a bit from what I have with the code I posted above ...
但是返回的错误是:“状态HTTP404。MyApp / MyApp / upload请求的资源不可用(在我的计算机上为法语)。它与上面发布的代码有点不同...
One likely difference is the webapp's context path . 一个可能的区别是webapp的上下文路径 。 In dev, the web app is deployed at root (
contextPath=/
) whereas in Tomcat it's using the name of your war file by default ( contextPath=/mywebapp/
). 在开发人员中,Web应用程序部署在根目录(
contextPath=/
),而在Tomcat中,它默认使用war文件的名称( contextPath=/mywebapp/
)。
Your form always posts to http://<server>:<port>/upload
but in Tomcat your servlet is at http://<server>:<port>/mywebapp/upload
. 您的表单始终发布到
http://<server>:<port>/upload
但是在Tomcat中,您的servlet在http://<server>:<port>/mywebapp/upload
。
You'll want to use: 您将要使用:
myUploadForm.setAction(GWT.getHostPageBaseUrl() + "/upload");
or possibly 或可能
myUploadForm.setAction("upload");
(without the leading slash) (不带斜杠)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.