Sunday, May 28, 2006

Java servlet

What is a servlet?

From Stefan Zeiger's novocode.com:
Servlets are modules of Java code that run in a server application (hence the name "Servlets", similar to "Applets" on the client side) to answer client requests. Servlets are not tied to a specific client-server protocol but they are most commonly used with HTTP and the word "Servlet" is often used in the meaning of "HTTP Servlet".

Labels:

Sunday, May 21, 2006

Struts and file upload

I'm trying to use a form that has a array of FormFile as a field of the form.

I can use to display the FormFiles with no problem, but when I submit the form, the array only has null values.

To be completely specific, I made a bean where one of the fields is a FormFile.
And my form contains an array of this bean.

When I submit the form, the action gets an array of my bean, but the FormFile attribute is null.

If I create a FormFile field directly in the form and submit it, it works as expected, giving me the uploaded file information.

Is it not possible in struts to upload a variable number of files?

I'm sure it's possible, but I have a feeling it'd be easier to just declare five FormFile fields and deal with that.

update: 2006-05-22:
I made two now obvious mistakes:

1) Need to set indexed="true" for the fields generated inside my iterate loop. This way, instead of generating (in my case) five < input name="uploadFile" type="file"> fields, I can generate
< input name="images[0].uploadFile" type="file">
< input name="images[1].uploadFile" type="file">
< input name="images[2].uploadFile" type="file">
< input name="images[3].uploadFile" type="file">
< input name="images[4].uploadFile" type="file">
where each field has a distinct name (except that there is no space between "<" and "input" normally; I added it so you could see my code).

2) My second mistake was setting the id attribute in the iterate tag to a string different from the name of my file array. My file array is called images, and I set the iterate id to "imageArray." This would generate five fields called imageArray[0].uploadFile, imageArray[1].uploadFile, etc. Since imageArray doesn't exist in my form bean, upon submission of the form the file data gets lost since there is no parameter to save imageArray data to.

Both of these mistakes are really obvious to me now, but I shouldn't have made them at all considering that I encountered the same issues half a year ago. Bleh.

update 2006-06-26: I did have to do a little fiddling to get the validation errors displayed properly. The html:errors tag sadly does not support iteration directly, so you have to use jsp expressions to get the index appropriately.

Labels: , ,

Saturday, May 13, 2006

yahoo mail has the most annoying ads

I should stop using it. There's this ad that displays a larger ad if you roll over it, obstructing the mail that I want to see. Using tabs in my browser pretty much guarantees that I roll over the stupid add. A short term solution is learning to use the firefox shortcuts.