[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 568
  • Last Modified:

Servlet not accepting form values

Hello,

I have form with this code:
<form name="form" action="upload" method="post" enctype="multipart/form-data" 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
0
princehyderabad
Asked:
princehyderabad
  • 10
  • 6
1 Solution
 
aozarovCommented:
multipart/form-data is normally needed when you upload files.
0
 
aozarovCommented:
Did you try without it?
0
 
aozarovCommented:
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
facchioneCommented:
<!--  <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">
0
 
aozarovCommented:
Hmm, I though this is because princehyderabad said he tried both combination...
0
 
princehyderabadAuthor Commented:
>>  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
0
 
aozarovCommented:
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
0
 
aozarovCommented:
0
 
princehyderabadAuthor Commented:
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"



0
 
aozarovCommented:
multipart data is encoded data. The servlets api does not decode it by itself.
This is why you have all those third party libraries that performs this.
If you don't need to upload files then don't put multipart/form-data. If  you do need to upload files
then it is better for you to use one of those libraries instead of decoding it yourself.
The Oreilly one is pretty small.
0
 
princehyderabadAuthor Commented:
I'm confused on Oreilly. May be bcoz lot of other classes....Any simple way ??
0
 
aozarovCommented:
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);
                    }
0
 
princehyderabadAuthor Commented:
Thanks for ur time I got it using
org.apache.commons.fileupload.DiskFileUpload.*;
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....
0
 
aozarovCommented:
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?
0
 
princehyderabadAuthor Commented:
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(storedProcCall1);
                        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.
0
 
aozarovCommented:
You confused me for a second.
Look at the other thread: http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21390904.html#13795847
for my last answer.
0
 
princehyderabadAuthor Commented:
I'm sorry .......lol
I already posted that Q before I asked U on this thread......

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now