dodowoman
asked on
how to upload a file and update other fields on the jsp form page at the same time?
experts,
I have a jsp form which has a "browse" button and many other fields. THe idea is when user click on "submit", if there is a file entry in the "browse" button text field entry, then the file should be uploaded to the server, and any other changes on the form should also get pass to the database.
I use a ContollerServlet to handle all the request from form, and pass the request to other classes to process the request and finally pass the result to JSP pages.
I have successfully make s the form work without "browse" button. but when the "browse" button is added, the submit failed. my questions are:
1. is it possible to pass the "request" class from JSP to java class? Is the "request" on JSP page same as "HttpServletRequest " in servlet doPost()?
if both request are referring the same thing, why is the parameters on the form could not be passed to the java class, by using
request.getParameter() in the servlet? is it because I have to use the form tag like this one:
<form action="<%=request.getCont extPath()% >/servlet/ ControlSer vlet?from= Upload" ENCTYPE="multipart/form-da ta" method="post" name="visits" onSubmit="return validate()" >
and the
ENCTYPE="multipart/form-da ta"
makes all the other parameters unaccessible by request.getParameter()?
thanks.
I have a jsp form which has a "browse" button and many other fields. THe idea is when user click on "submit", if there is a file entry in the "browse" button text field entry, then the file should be uploaded to the server, and any other changes on the form should also get pass to the database.
I use a ContollerServlet to handle all the request from form, and pass the request to other classes to process the request and finally pass the result to JSP pages.
I have successfully make s the form work without "browse" button. but when the "browse" button is added, the submit failed. my questions are:
1. is it possible to pass the "request" class from JSP to java class? Is the "request" on JSP page same as "HttpServletRequest " in servlet doPost()?
if both request are referring the same thing, why is the parameters on the form could not be passed to the java class, by using
request.getParameter() in the servlet? is it because I have to use the form tag like this one:
<form action="<%=request.getCont
and the
ENCTYPE="multipart/form-da
makes all the other parameters unaccessible by request.getParameter()?
thanks.
http://www.onjava.com/pub/a/onjava/2003/06/25/commons.html?page=3
ASKER
thanks, but what is the advantage of using FileUpload class instead of using MultipartRequest class, do you know?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
download api from http://jakarta.apache.org/commons/fileupload/
ASKER
thanks, but I am trying to fix someone else's bug, where is using MultipartRequest class.
this is the codes, please help. i can't see the problem:
************************** ********** *******
try {
multi = new MultipartRequest(req, FILE_PATH, 500 * 1024
, new com.oreilly.servlet.multip art.Defaul tFileRenam ePolicy()) ;
}catch (Exception e) {
e.printStackTrace();
error = e.toString();
}
Enumeration files = null;
try{
files = multi.getFileNames();
}catch(Exception e)
{
e.printStackTrace();
}
String fileParam = null;
try{
while (files.hasMoreElements())
fileParam = (String) files.nextElement();
}catch(Exception e)
{
e.printStackTrace();
}
savedFileName = multi.getFilesystemName(fi leParam);/ / get the saved file name
if (savedFileName!=null && savedFileName.length()>0)/ / if the file saved successfully
session.setAttribute("file ", savedFileName);
else session.setAttribute("file ", "");
************************** ********** ********
the file always return "null", and not throwing any exception.why?
this is the codes, please help. i can't see the problem:
**************************
try {
multi = new MultipartRequest(req, FILE_PATH, 500 * 1024
, new com.oreilly.servlet.multip
}catch (Exception e) {
e.printStackTrace();
error = e.toString();
}
Enumeration files = null;
try{
files = multi.getFileNames();
}catch(Exception e)
{
e.printStackTrace();
}
String fileParam = null;
try{
while (files.hasMoreElements())
fileParam = (String) files.nextElement();
}catch(Exception e)
{
e.printStackTrace();
}
savedFileName = multi.getFilesystemName(fi
if (savedFileName!=null && savedFileName.length()>0)/
session.setAttribute("file
else session.setAttribute("file
**************************
the file always return "null", and not throwing any exception.why?
THNKS MATE :)