Buffort
Buffort

Reputation: 55

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.

I am actually coding a web app with Java and GWT for my research institute. The goal is to produce an XML file containing metadata from user inputs, after flight campaigns. 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).

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). Not really complex.

Using Eclipse 4.4 and the embedded Jetty server, no problem at all ! It's really nice, the XML file is well formated, the download and upload functions are ok. 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. 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 ...

Here is the part of my main code to call the 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:

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:

<?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. With Eclipse, no issue, but Tomcat can't find the default 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).

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.

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). Then I compress this directory to a .zip file and changed ".zip" to ".war". This .war file is then copy/past to the "webapps" directory of Tomcat. Tomcat process the .war file and I obtain a directory calle "MyApp", in which I have another "MyApp" directory containing all my files. Finally, I launch Firefox and access my app with the following Url : 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. 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 ...

Upvotes: 2

Views: 1672

Answers (1)

Thomas Broyer
Thomas Broyer

Reputation: 64561

One likely difference is the webapp's context path. 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/).

Your form always posts to http://<server>:<port>/upload but in Tomcat your servlet is at http://<server>:<port>/mywebapp/upload.

You'll want to use:

myUploadForm.setAction(GWT.getHostPageBaseUrl() + "/upload");

or possibly

myUploadForm.setAction("upload");

(without the leading slash)

Upvotes: 5

Related Questions