princehyderabad
asked on
Servlet not accepting form values
Hello,
I have form with this code:
<form name="form" action="upload" method="post" enctype="multipart/form-da ta" onsubmit="return validate();">
<!-- <input type="hidden" name="dir" value="abc"> -->
<% String a = "abc";
request.setAttribute("dir" ,a);%>
=====My Servlet ====
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {
String f = req.getParameter("dir");
System.out.println(f);
========================
I get null value. I trying 1st by using hidden value didnt worked. They I tried using request.setAttribute, still didnt worked.
Anything special in servlet code to write when the form enctype is multipart/form-data
Regards
Hyd
I have form with this code:
<form name="form" action="upload" method="post" enctype="multipart/form-da
<!-- <input type="hidden" name="dir" value="abc"> -->
<% String a = "abc";
request.setAttribute("dir"
=====My Servlet ====
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException, ServletException {
String f = req.getParameter("dir");
System.out.println(f);
========================
I get null value. I trying 1st by using hidden value didnt worked. They I tried using request.setAttribute, still didnt worked.
Anything special in servlet code to write when the form enctype is multipart/form-data
Regards
Hyd
multipart/form-data is normally needed when you upload files.
Did you try without it?
BTW, <% String a = "abc"; request.setAttribute("dir" ,a);%>
This is assign "dir" request atrribute to "abc" for that current request.
Though it can be accessed by calling req.getAttribute("dir"), and not by calling getParameter (which is for getting parameters the new request), it is definetly not what you want to do in this case.
This is assign "dir" request atrribute to "abc" for that current request.
Though it can be accessed by calling req.getAttribute("dir"), and not by calling getParameter (which is for getting parameters the new request), it is definetly not what you want to do in this case.
<!-- <input type="hidden" name="dir" value="abc"> -->
is inside an .html comment, which is your problem. remove the <!-- and --> and it should work:
<input type="hidden" name="dir" value="abc">
is inside an .html comment, which is your problem. remove the <!-- and --> and it should work:
<input type="hidden" name="dir" value="abc">
Hmm, I though this is because princehyderabad said he tried both combination...
ASKER
>> multipart/form-data is normally needed when you upload files
Without "multipart/form-data" I can able to get the values but my upload files will not work.
I also tried in Servlet
String f = (String) req.getAttribute("dir");
and
String f = req.getParamater("dir");
Still null values.
facchione I wantedly comment that to see if it works with setAttribute
Without "multipart/form-data" I can able to get the values but my upload files will not work.
I also tried in Servlet
String f = (String) req.getAttribute("dir");
and
String f = req.getParamater("dir");
Still null values.
facchione I wantedly comment that to see if it works with setAttribute
So if you want to both upload file and read parametrs then you better of using some library that will the hard work for you.
Recommended one is: http://jakarta.apache.org/commons/fileupload/
See their user guide for example: http://jakarta.apache.org/commons/fileupload/using.html
Recommended one is: http://jakarta.apache.org/commons/fileupload/
See their user guide for example: http://jakarta.apache.org/commons/fileupload/using.html
Another famous one is: http://servlets.com/cos/javadoc/com/oreilly/servlet/MultipartWrapper.html
ASKER
All your links have soo many files need to install run and go thru etc.,
I need a very simple stuff but in correct way. Only thing I need is, in my serlvet how to accept the "setAttribute" values or passing values thru hidden form fileds using form as POST method and ecntyp as "multipart/form-data"
I need a very simple stuff but in correct way. Only thing I need is, in my serlvet how to accept the "setAttribute" values or passing values thru hidden form fileds using form as POST method and ecntyp as "multipart/form-data"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I'm confused on Oreilly. May be bcoz lot of other classes....Any simple way ??
Look at this link: http://www.jguru.com/faq/view.jsp?EID=1045507
it has an example of how to use the Oreilly class.
All you need to do is to the add (from Oreilly website) the jar file http://servlets.com/cos/cos-05Nov2002.zip to your WEB-INF/lib folder
and then in your upload servlet do somthing like that (you need to use only two classes MultipartParser and Part) :
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
...
MultipartParser mp = new MultipartParser(request, 1*1024*1024); // 10MB
Part part;
while ((part = mp.readNextPart()) != null) {
String name = part.getName();
if (part.isParam()) { // handle regular parameter
// it's a parameter
ParamPart paramPart = (ParamPart) part;
String value = paramPart.getStringValue() ;
out.println("param; name=" + name + ", value=" + value);
}
else if (part.isFile()) { // handle upload file
// it's a file part
FilePart filePart = (FilePart) part;
String fileName = filePart.getFileName();
if (fileName != null) {
// the part actually contained a file
long size = filePart.writeTo(dir);
out.println("file; name=" + name + "; filename=" + fileName +
", filePath=" + filePart.getFilePath() +
", content type=" + filePart.getContentType() +
", size=" + size);
}
it has an example of how to use the Oreilly class.
All you need to do is to the add (from Oreilly website) the jar file http://servlets.com/cos/cos-05Nov2002.zip to your WEB-INF/lib folder
and then in your upload servlet do somthing like that (you need to use only two classes MultipartParser and Part) :
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
...
MultipartParser mp = new MultipartParser(request, 1*1024*1024); // 10MB
Part part;
while ((part = mp.readNextPart()) != null) {
String name = part.getName();
if (part.isParam()) { // handle regular parameter
// it's a parameter
ParamPart paramPart = (ParamPart) part;
String value = paramPart.getStringValue()
out.println("param; name=" + name + ", value=" + value);
}
else if (part.isFile()) { // handle upload file
// it's a file part
FilePart filePart = (FilePart) part;
String fileName = filePart.getFileName();
if (fileName != null) {
// the part actually contained a file
long size = filePart.writeTo(dir);
out.println("file; name=" + name + "; filename=" + fileName +
", filePath=" + filePart.getFilePath() +
", content type=" + filePart.getContentType() +
", size=" + size);
}
ASKER
Thanks for ur time I got it using
org.apache.commons.fileupl oad.DiskFi leUpload.* ;
I used above class methods to get fileds from form.
Can you help on this:
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
System.out.println("Name:" +name+ " Value:" +value);
}
Above code gives me form fileds names ...
Name: a Value: 1
Name: b value : 2
...
But is there a way to store value 1, 2, .... in a String variable. How ??
I'm confused bcoz it whole thing going in loops....
org.apache.commons.fileupl
I used above class methods to get fileds from form.
Can you help on this:
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
System.out.println("Name:"
}
Above code gives me form fileds names ...
Name: a Value: 1
Name: b value : 2
...
But is there a way to store value 1, 2, .... in a String variable. How ??
I'm confused bcoz it whole thing going in loops....
Not sure I understand you.
If you want to keep the values to be used after the loop then
add them to a map.
Map params = new HashMap();
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
map.put(name, value);
System.out.println("Name:" +name+ " Value:" +value);
}
..
}
Now after the loop you can do: map.get("a") which will return "1".
Is this what you need?
If you want to keep the values to be used after the loop then
add them to a map.
Map params = new HashMap();
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
map.put(name, value);
System.out.println("Name:"
}
..
}
Now after the loop you can do: map.get("a") which will return "1".
Is this what you need?
ASKER
Once we add it to MAP, how to retrive and pass here:
Map params = new HashMap();
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
map.put(name, value);
System.out.println("Name:" +name+ " Value:" +value);
}
else
{
String storedProcCall1 = "{call sp (?,?,?)}";
CallableStatement cs1 = connection.prepareCall(sto redProcCal l1);
File cfile=new File(item.getName());
cs1.setString(1,<<HOW TO PASS VALUE params1 HERE'>>);
cs1.setString(2,<<HOW TO PASS VALUE params2 HERE'>>);
cs1.setString(3,<<HOW TO PASS VALUE params3 HERE'>>);
}
Conserding that user send only 3 filed in form.
Map params = new HashMap();
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField())
{
String name = item.getFieldName();
String value = item.getString();
map.put(name, value);
System.out.println("Name:"
}
else
{
String storedProcCall1 = "{call sp (?,?,?)}";
CallableStatement cs1 = connection.prepareCall(sto
File cfile=new File(item.getName());
cs1.setString(1,<<HOW TO PASS VALUE params1 HERE'>>);
cs1.setString(2,<<HOW TO PASS VALUE params2 HERE'>>);
cs1.setString(3,<<HOW TO PASS VALUE params3 HERE'>>);
}
Conserding that user send only 3 filed in form.
You confused me for a second.
Look at the other thread: https://www.experts-exchange.com/questions/21390904/Logic-Correction-Servlet.html#13795847
for my last answer.
Look at the other thread: https://www.experts-exchange.com/questions/21390904/Logic-Correction-Servlet.html#13795847
for my last answer.
ASKER
I'm sorry .......lol
I already posted that Q before I asked U on this thread......
I already posted that Q before I asked U on this thread......