File Upload Problem with GWT And Java HttpServlet

713 views
Skip to first unread message

Nickelnext

unread,
Apr 7, 2009, 1:41:32 PM4/7/09
to Google Web Toolkit
Hello everyone,

this is my first post and i pray i did not do anything wrong by write
it here.
I've been looking for the solution of my problem for days over the
web, but i did not find what i need so, i'm posting right here at
google groups hoping someone more skilled than me and very patient
could help me.

Before start, i let u know how my workspace is configured:
package: com.my.site.myprojects.provaupload
public >>css, html,
ProvaUpload.gwt.xml
package: com.my.site.myprojects.provaupload.client
>>ProvaUpload.java
package: com.my.site.myprojects.provaupload.server
>>MyFormHandler.java

Now, here's my problem:

I need an upload form to upload a small file on the server, i have my
simple ProvaUpload.java like this

public void onModuleLoad() {
final FormPanel form = new FormPanel();
form.setAction(GWT.getModuleBaseURL() + "/myFormHandler");

// Because we're going to add a FileUpload widget, we'll need
to set the
// form to use the POST method, and multipart MIME encoding.
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);

VerticalPanel panel = new VerticalPanel();
form.setWidget(panel);

// Create a FileUpload widget.
FileUpload upload = new FileUpload();
upload.setName("uploadFormElement");

panel.add(upload);
Button button2 = new Button("Submit", new ClickListener() {
public void onClick(Widget sender) {
form.submit();
}
});
// Add a 'submit' button.
panel.add(button2);

// Add an event handler to the form.
form.addFormHandler(new FormHandler() {

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).
Window.alert(event.getResults());
// Window.alert("Done");
}

public void onSubmit(FormSubmitEvent event) {
// TODO Auto-generated method stub
Window.alert("upload in progress");
}
});
RootPanel.get().add(form);
}

found the code over the web.

Now my problem is here: i don't need any of the gwt (ajax) async
callback or anything: i only need a simple servlet that takes care of
my uploaded file, so my MyFormHandler.java is like this

public class MyFormHandler extends HttpServlet {

private static final long serialVersionUID = 1L;

public void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {

resp.setContentType("text/html");

FileItem uploadItem = getFileItem(req);
if(uploadItem == null) {
resp.getWriter().write("NO-SCRIPT-DATA");
return;
}

resp.getWriter().write(new String(uploadItem.get()));
}
private FileItem getFileItem(HttpServletRequest req) {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setFileSizeMax(100000000);
try {
List items = upload.parseRequest(req);
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;
}http://groups.google.com/group/Google-Web-Toolkit/
browse_thread/thread/50b6a7fee3262a18

return null;
}

}

also this one found on the web. That's right, i should have done it by
myself but i'm trying to figure out how the whole gwt works so...no
time for it.

Now, what i don't understand which of the two files (the web.xml one
or the ProvaUpload.gwt.xml one) i have to change (do i need to add the
<servlet ... /> in the second one or must i call the <servlet name>
<servlet mapping> in the first one?
And in which way? Because i tried a lot, but the only answer i get is
" [WARN] Resource not found: myFormHandler; (could a file be missing
from the public path or a <servlet> tag misconfigured in module
xxx.xxx.xxx.provaupload.ProvaUpload.gwt.xml ?)"

I'll post also the content of my two xml files so you can check:

web.xml (in blablabla/tomcat/web-inf)
<servlet>
<servlet-name>MyFormHandler</servlet-name>
<servlet-
class>xxx.xxx.xxx.myprojects.provaupload.server.MyFormHandler</servlet-
class>
</servlet>
<servlet-mapping>
<servlet-name>MyFormHandler</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

and the blabla.gwt.xml

<servlet path="/myFormHandler" class="/
xxx.xxx.xxx.myprojects.provaupload.server.MyFormHandler"/>

So...i appreciate any help, and hope at least one of you survived to
this long post.
Thank you all in advance and sorry for the bad english

- Nickelnext

Isaac Truett

unread,
Apr 7, 2009, 2:40:54 PM4/7/09
to Google-We...@googlegroups.com
Welcome!

I think your problem could be as simple as the "/" at the start of
your class name:

<servlet path="/myFormHandler" class="/
xxx.xxx.xxx.myprojects.provaupload.server.MyFormHandler"/>

Try changing it to this:

<servlet path="/myFormHandler"
class="xxx.xxx.xxx.myprojects.provaupload.server.MyFormHandler"/>

Now, if that isn't the problem (maybe the "/" was a typo in your post,
but not in your gwt.xml), then I would ask you to make sure that you
are compiling your servlet (javac from the command line, or an IDE
such as Eclipse will do this automatically). The compiled servlet
class needs to be on your classpath when you run hosted mode.

Hope that helps.

- Isaac

Nickelnext

unread,
Apr 7, 2009, 3:15:36 PM4/7/09
to Google Web Toolkit
Hello Isaac

thank you very much for your quick reply!
That wasn't for sure the "/" 'cause i tried with and without it but it
never changed.

What i was surely doing wrong was the fact that i did not compile the
servlet. I stupidly thought that gwt did it!
Now i'm downloading j2ee from sun.com (yeah, i never used servlets
before, did you notice that?) and i'll try soon to compile and get the
results. I'll post soon an update.

Thank you again, for now
- Nickelnext

Nickelnext

unread,
Apr 7, 2009, 5:11:02 PM4/7/09
to Google Web Toolkit
Hello!

Now...i did the compilation with javac like:

javac -cp lib1.jar:lib2.jar:lib3.jar MyFormHandler.java and it went
well. Now in server folder i have the .class file. But...what now? If
i run in hosted mode it doesn't work either. The error is the same as
usual....what do i do wrong?

I also try to compile, but the result is similar: but here's another
question: do i need to create a war file and upload it to my tomcat
server or is it enough to go to www directory (under the ProvaUpload
dir) and open the html file from there?

Nickelnext

unread,
Apr 8, 2009, 6:17:35 AM4/8/09
to Google Web Toolkit
Hello everyone!
sorry for the multiple posting, but this morning i updated everything
to the 1.6, downloaded the eclipse plugin and so on.
Now i'm getting another error:

The code is the same as yesterday. What i get now is a message in the
window.alert like this: "HTTP ERROR: 404 NOT_FOUND RequestURI=/
provaupload2 MyFormHandler
Powered by Jetty://"

I really don't understand how and where gwt look for classes or paths.
I configured the web.xml with the servlet like the old one. Where
should i put the Servlet?
And shall i compile it by myself or is gwt doing already?

Sorry for this dumb questions but i really don't find an explanation
anywhere.

Thank you
- Nickelnext

Isaac Truett

unread,
Apr 8, 2009, 9:45:15 AM4/8/09
to Google-Web-Toolkit
If you're running in web mode or hosted mode with -noserver, you would
deploy class files to WEB-INF/classes. com.example.Foo would be
WEB-INF/classes/com/example/Foo.class, for example.


2009/4/8 Nickelnext <nicke...@gmail.com>:

Nickelnext

unread,
Apr 9, 2009, 8:44:30 AM4/9/09
to Google Web Toolkit
Thank you for your help Isaac, i really appreciate it.
So, if u let me, i'll resume a bit my situation so it will be more
clear for everyone.

I changed the web.xml pointing to the FormHandler which is in
the .server package. I also changed the module.gwt.xml to match the
servlet (i read it in the
com.google.gwt.eclipse.sdkbundle.linux_1.6.4.v200904062254/gwt-
linux-1.6.4/doc/helpInfo/servletMappings.html).
So now my servlet should be created (and compiled) with a .class file
(i see it in the war/WEB-INF/classes/com/my/site/myprojects/
provaupload2/ProvaUpload2/server/ folder).

What i cannot do is to configure the setAction properly (i tried with
"/MyFormHandler", with the whole path "WEB-INF/CLASSES/.../server/
MyFormHandler", with and without using the GWT.getModuleBaseUrl() and
the others two .get methods provided by GWT. But it's always the same
404 not found.

Should i run with -noserver instead of running with eclipse and the
pressing the "compile/browse" button?
Do i miss something? Surely something stupid...

Thank you again for your help.
- Nickelnext

On 8 Apr, 15:45, Isaac Truett <itru...@gmail.com> wrote:
> If you're running in web mode or hosted mode with -noserver, you would
> deploy class files to WEB-INF/classes. com.example.Foo would be
> WEB-INF/classes/com/example/Foo.class, for example.
>
> 2009/4/8Nickelnext<nickeln...@gmail.com>:

Nickelnext

unread,
Apr 9, 2009, 8:34:45 AM4/9/09
to Google Web Toolkit
Hello Isaac, hello everyone

First of all, I'd like to thank you for your help, i really appreciate
Message has been deleted
Message has been deleted

Isaac Truett

unread,
Apr 9, 2009, 9:16:38 AM4/9/09
to Google-We...@googlegroups.com
Hi Nickelnext,

> What i cannot do is to configure the setAction properly (i tried with
> "/MyFormHandler", with the whole path "WEB-INF/CLASSES/.../server/
> MyFormHandler", with and without using the GWT.getModuleBaseUrl() and
> the others two .get methods provided by GWT. But it's always the same
> 404 not found.

So what you're looking for here is a URL relative to your HTML page
that matches your servlet mapping in web.xml.

Let's say you have this URL pattern in web.xml:

<url-pattern>/myFormHandler</url-pattern>

and your application is deployed here:

http://localhost:8080/myApp/index.html

Then the URL for your servlet would be:

http://localhost:8080/myApp/myFormHandler

So you would call setAction() like so:

form.setAction(GWT.getModuleBaseURL() + "myFormHandler");

Where GWT.getModuleBaseURL() returns the path to your HTML, without
the file name:

http://localhost:8080/myApp/

See how that all comes together?

From your description, it sounds like you may already have tried this
setup. But I suspect you might be changing your web.xml directly in
the tomcat/webapps/myApp directory instead of going through a
build/deploy cycle. If that's so, then Tomcat may not be recognizing
your changes and reconfiguring your application before your next test.
So, you'll want to either adopt a build process (Ant's my tool of
choice) or be sure to restart the webapp through the Tomcat manager
(or you could restart Tomcat if that's easier).

Can you setup your web.xml and form action similar to the above, make
sure your changes get deployed, and then let me know if that's
working?

- Isaac

Nickelnext

unread,
Apr 9, 2009, 1:21:16 PM4/9/09
to Google Web Toolkit
First: sorry for the multiple posting: there were some problems on
this topic.
Second: i followed some of yours advices. I made these changes

-> in web.xml (war/subfolder) in the url pattern i put something like
this: /provaupload2/MyFormHandler
-> in module.gwt.xml (in src/ subfolder) i put this <servlet path="/
MyFormHandler"
class="com.my.site.myprojects.provaupload2.ProvaUpload2.server.MyFormHandler"/
>
-> in ProvaUpload2.java (src/..client subfolder) in the setAction
method i did this: "form.setAction(GWT.getModuleBaseURL() +
"MyFormHandler");

Now it seems that the servlet is been called. I Have a " HTTP ERROR:
500 access denied (java.io.FilePermission /tmp/
upload__68c5f414_1208bd2536b__8000_00000000.tmp write) "
but i guess it's an error caused by some hosted/compiled mode
restriction.
Now i'll try to pack everything up in a .war file and deploy it on my
webserver and see what happens.
I'll give you an update when i'll have done it.
If you already know what is the error i have, please let me know.
I hope there will be only a few of other thing to fix to make the
whole program running!

Thank you again for your time and your help!!
- Nickelnext

Nickelnext

unread,
Apr 9, 2009, 1:56:21 PM4/9/09
to Google Web Toolkit
Update: i did a jar -cvf lol.war * in the /war/ folder, i did deploy
it to the tomcat server but when i press the submit button -> "HTTP
Status 404 - /provaupload2/MyFormHandler"

- Nickelnext

Isaac Truett

unread,
Apr 9, 2009, 3:54:55 PM4/9/09
to Google-We...@googlegroups.com
Couple of questions:

1. What is the URL pattern of your servlet mapping?
2. What is your application context path?

Nickelnext

unread,
Apr 10, 2009, 4:08:24 AM4/10/09
to Google Web Toolkit
Hello Isaac.

About my last update: I figured out what i was doing wrong with the
apache/tomcat server. Simply i gave the "lol.war" name instead of
"provaupload2.war". Now it works like the hosted/compiled mode. Now i
get the http 500 error but withouth the "RequestURI=/provaupload2/
MyFormHandler", which i always get in hosted/compiled mode.

Also, what i do not understand is that in the hosted/compiled mode i
have a path (in the browser url bar) like this: "localhost:port/
provaupload2.html", instead of "localhost:port/provaupload2/", which i
have when i deploy the .war file in my apache/tomcat "native" server.

How am i supposed to change this?

About your questions:
1 - i suppose that url-pattern refers to the web.xml <url-pattern>, so
this is it: <url-pattern>/provaupload2/MyFormHandler</url-pattern>
2 - i think i have answered to this question in the first lines.
Another time: what i can't do is to run the application in hosted/
compiled out of the localhost:port/provaupload2.html path.

Last: i'm pretty sure that the 500 error i get is caused by permission/
folder problems. I'm trying to figure out how to fix this.

I hope i gave you the information you need, sorry but i'm not really
skilled with servlets and apache!
Thank you again
- Nickelnext

Nickelnext

unread,
Apr 10, 2009, 4:33:33 AM4/10/09
to Google Web Toolkit
Update: there was a biiiiiiiiig error in the HttpServlet code:
the main method was service instead of doPost. I fixed it and now the
hosted/compiled mode works fine! I can see the bytes uploaded through
the response.write.blablabla.

What is not working is the deployed file on my "native" apache/tomcat
server, there's always an access denied permission exception.
So now i'm looking for a solution to permission problems in apache.
I think i've read it somewhere here on google groups.

- Nickelnext

Isaac Truett

unread,
Apr 10, 2009, 8:21:09 AM4/10/09
to Google-We...@googlegroups.com
> Also, what i do not understand is that in the hosted/compiled mode i
> have a path (in the browser url bar) like this: "localhost:port/
> provaupload2.html", instead of "localhost:port/provaupload2/", which i
> have when i deploy the .war file in my apache/tomcat "native" server.
>
> How am i supposed to change this?

As you've already seen, that's your application context. If you want
the "root" context (localhost:port/provaupload2.html) then you would
deploy your application to webapps/ROOT.
Reply all
Reply to author
Forward
0 new messages