Solved

req.getParameter gives me null

Posted on 2001-06-27
13
225 Views
Last Modified: 2010-03-31
I found a script for a file Upload, it works fine (by the way, its a servlet).  

The problem that I have is that I can *only* get the contents of the file field! req.getParameter("");

I have seen this problem before ( and i noticed that it seemed to be because of the enctype being set to multipart form data.

How do I get values of other fields from my Servlet ?

cheers.
0
Comment
Question by:mehdi
13 Comments
 
LVL 2

Expert Comment

by:jorj
ID: 6230772
some piece of code, maybe ? I'm not sure of what are you talking about...
0
 
LVL 1

Author Comment

by:mehdi
ID: 6230824
Here is the exact code !

http://home.swipnet.se/~w-50670/yafus.html

its works perfectly, but.. if I have another Form element (input type of text), i can not retrieve its value from the servlet, it returns null (req.getParameter does not work with the nectype of the form)

~~~~~~~~~~~~~~~~~~~~~~~~~~~
The secret lemonade drinker
0
 
LVL 1

Author Comment

by:mehdi
ID: 6230827
i meant enctype :)
0
 
LVL 15

Expert Comment

by:mohan_sekar
ID: 6230830
hi,

   I am not getting ur question dear. More elaborate, better with some piece of code

bye
0
 
LVL 1

Author Comment

by:mehdi
ID: 6230862
umm.. ok..

See the link that I sent you.  That is the code.  It works fine.

I want to add to the servlet in question to get other inputs from the form aswel.. (imagine I also had <input type="text" name = "NAME" value="THIS IS A VALUE"> )

so, where in the servlet (SEE LINK I POSTED) it says

HTMLout.println("<center><h2>The file \'"+FileName+"\' has been uploaded.</h2></center>");

I want to retrieve the value of another HTML element (NAME, above) within my servlet.. so I change the above line to:

HTMLout.println("<center><h2>The file \'"+FileName+"\' has been uploaded.  Your name is " + _req.getParameter("NAME") + "</h2></center>");

my problem is that _req.getParameter("NAME") gives me null.

Im sorry if that is not clear enough, I have explained it ad nausia now :]

~~~~~~~~~~~~~~~~~~~~~~~~~~~
The secret lemonade drinker

0
 
LVL 2

Expert Comment

by:jorj
ID: 6231042
A quick advice:
do a test by printing all information you receive and see if the parameter you are looking for is sent.

Enumeration eHeader = request.getHeaderNames ();
while(eHeader.hasMoreElements ())
{
     String key = (String)eHeader.nextElement ();
     String value = request.getHeader (key);
     System.out.println ("\t" + key + " : " + value);
}

Do the test also with request.getAttributeNames() and request.getParameterNames() and you will see if and where is what you are looking for.

0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Expert Comment

by:berserks
ID: 6231480
It cannot be done.  You have 2 different forms and you're only submitting one form.  In order for a servlet to be able to retrieve data from a form, you'll have to submit it to the servlet.  

One way I would suggest is to write an onclick that submits both forms, then you can probably retrieve data from both forms.  If that doesn't work, you'll have to submit the first form to the second form, retrieve data from the first form in the second form, and submit the second form.
0
 
LVL 2

Expert Comment

by:jorj
ID: 6231543
Of course he can't with two different forms, but I believe he would want to add new elements to the same form and here is where he's in trouble.
0
 
LVL 1

Author Comment

by:mehdi
ID: 6231551
jorj

Yes, thats where the problem lies ! I have solved this bu doing the upload completely seperately (as seenon hotmail, yahoo etc).  I never had this problem in Perl.  CGI.pm always took care of this.
0
 
LVL 2

Expert Comment

by:jorj
ID: 6231714
So you actualy don't know where were you stack in ? Because if you do, please let an answer for those to come.

Hope I was useful.
0
 
LVL 2

Expert Comment

by:jorj
ID: 6237063
Your problem was one I was interested in and never had the time to implement it. So I decided to study what's going on there and here is the complete answer (not for points but for future references).

The protocol:
http://www.w3.org/TR/1998/REC-html40-19980424/interact/forms.html#h-17.13.4.2

In html file:
<INPUT TYPE="hidden" name="ascuns" VALUE="myvalue">
TYPE can be TEXT also. You forgot to close the FRAME tag in the example you gave us !


Result, as it is received char by char, except the "Headers: and "Body:" labels. As you see you can send as many parameters as you want and you'll find them in the same way as you have found the filename.

Headers:
     Accept : image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/pdf, */*
     Accept-Language : en-us
     Content-Type : multipart/form-data; boundary=---------------------------7d13422aa05de
     Accept-Encoding : gzip, deflate
     User-Agent : Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)
     Host : localhost:8080
     Content-Length : 757
     Connection : Keep-Alive

Body:

-----------------------------7d13422aa05de
Content-Disposition: form-data; name="upload"; filename="C:\FileUpload\test.xxx"
Content-Type: text/html

text-file-content-here

-----------------------------7d13422aa05de
Content-Disposition: form-data; name="ascuns"

myvalue

-----------------------------7d13422aa05de--
0
 
LVL 5

Expert Comment

by:vemul
ID: 7616550
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- To be PAQ'ed and points refunded
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

vemul
Cleanup Volunteer
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 7639847
Finalized as proposed

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now