drew drew - 5 months ago 11
HTML Question

Weird issue when using HTML form to pass information to Java Servlet

Okay so I have two Java Servlets, one for letting the user select which images to delete (

DeleteImages
) and another for actually deleting the images (
HandleDelete
).
DeleteImages
displays all the images in the container with a checkbox HTML form for the user to select which images to delete. Then, using POST the servlet passes that information along to
HandleDelete
which iterates over which images it received and deletes them.

I actually had this working, but then I tried to change the structure of the code (have
DeleteImages
forward to a .jsp file that output the HTML form which would then forward to
HandleDelete
) but that didn't work out, so I'm trying to go back to the old way and now it's not working even though I'm pretty sure it's the same as I had before.

From
DeleteImages
:

// retrieve image files
List<? extends SwiftObject> objs = os.objectStorage().objects().list("imageFiles");

out.println("<!DOCTYPE html><html><head><title>Object Storage - Delete</title><link rel='stylesheet' href='stylesheet.css' type='text/css' /></head>"
+ "<body>
<h1>Select images to Delete from container</h1>
<form method='POST' enctype='multipart/form-data' action='/ImageUpload/OSHandleDelete'>");

for (SwiftObject o : objs) {
// omitted code that gets the image's name, date last modified, and filepath (all Strings, I know this works)

out.println("<b>Name:</b> " + name + "<br/> <b>Time of Upload:</b> " + date + "<br/>"
+ "<input type='checkbox' name='"+name+"'> "
+ "<img src='"+filepath+"' alt='' style='max-width:800px;' /> <br/> <br/> <br/> ");
}

out.println("<input type='submit' value='Delete Images' /></form>");
out.println("</form> <br/> <br/> <div><br/><a class='return' href='index.jsp'><b>Click here to return home</b></a><br/><br/></div> <br/> <br/></body></html>");


From
HandleDelete
:

Enumeration<String> parameterNames = request.getParameterNames();
if (!parameterNames.hasMoreElements())
System.out.println("no parameters (null)");

while (parameterNames.hasMoreElements()) {

String paramName = parameterNames.nextElement();
System.out.println("*****************");
System.out.println("parameter: " + paramName);

String[] vals = request.getParameterValues(paramName);
if (vals == null)
System.out.println(" vals is null for " + paramName);
else {
for (int i = 0; i < vals.length; i++) {
System.out.println(" vals["+i+"]: " + vals[i]);
}
}


Right now I don't have
HandleDelete
actually doing anything besides print statements. This is because I use
request.getParameter("<name>")
to find out whether the user checked image of
<name>
(i.e. if it's null it was not checked but if it's not null it was checked).

The HTML form displays perfectly with the images and everything. My problem is that no matter what's checked in the HTML form,
HandleDelete
always prints to the console
no parameters (null)
meaning nothing was passed from
DeleteImages
to
HandleDelete
. I have a feeling the problem comes from either (1) the setAttribute statement in
DeleteImages
or (2) something with the HTML form. I've done a lot of searching and I'm pretty confident what I have is right though and I really can't figure out what's causing this issue (especially since I'm pretty sure this is exactly what I had before and it worked). Does anyone have any ideas?

Answer

I found the error: I included enctype=multipart/form-data

Not exactly sure why that caused the error but I removed that part and now it's passing the parameters perfectly. Thanks for your help guys!

Comments