This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes getting null parameters in post Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting null parameters in post" Watch "getting null parameters in post" New topic
Author

getting null parameters in post

khushi paul
Greenhorn

Joined: Feb 08, 2005
Posts: 26
hi all,
i have written a HTML forms body in doGet method of a servlet and trying to retriev form element values in doPost method of the same servlet.my problem is that some times i get the values by using request.getParameter but many times it just returns null although value has been entered in the form elements.
can anybody pls tell me its reason and possible solution?
regards
khushi
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613
kindly post your code


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
khushi paul
Greenhorn

Joined: Feb 08, 2005
Posts: 26
here's the code

public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
out.println("<form name=esubmit ENCTYPE='multipart/form-data' method=Post>");

some queries...............

out.println("<p> Div Initials: <INPUT TYPE=\"text\" NAME=\"dinitl\" value='"+rs.getString("r_divn_abrv")+"'> Sec Initials: <INPUT TYPE=\"text\" NAME=\"sinitl\" value="+rs.getString("r_secn_abrv")+"> Indentors Initials: <INPUT TYPE=\"text\" SIZE=\"5\" maxlength=\"5\" NAME=\"iinitl\" > Year: <INPUT TYPE=\"text\" SIZE=\"5\" maxlength=\"5\" NAME=\"year\" value="+pyear+"> Indent SrNo: <INPUT TYPE=\"text\" SIZE=\"5\" maxlength=\"5\" NAME=\"srno\"> <INPUT TYPE='file' NAME='document'> </p> ");
out.println("<INPUT TYPE='submit' VALUE='Save'>");

public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><body>");
out.println("init="+req.getParameter("iinitl"));

DiskFileUpload upload = new DiskFileUpload();
try{

//Parse the request
List items = upload.parseRequest(req);

// Process the uploaded items
Iterator iter = items.iterator();

while (iter.hasNext()) {
item = (FileItem) iter.next();
if (item.isFormField()) {
String fld=item.getFieldName();
String value=item.getString();
if(fld.equals("dinitl"))
{di=value;}
if(fld.equals("sinitl"))
{si=value;}
if(fld.equals("iinitl"))
{ii=value;}
}
}
out.println(ii);
....

do you need any more explanation?
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613
ENCTYPE='multipart/form-data'

I dought if you can use HttpRequest object with multipart..try MultiPartRequest
-Praful
[ March 14, 2005: Message edited by: Praful Thakare ]
khushi paul
Greenhorn

Joined: Feb 08, 2005
Posts: 26
thanks,but can u pls give me some code so that i can understand it properly?
and why is it so that my code works some times?
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 613
what tool are you using for upload ?
not sure if following classes are with you,chk out the respective calles in your tool for upload

if (MultipartFormDataRequest.isMultipartFormData(request))
{

// Uses MultipartFormDataRequest to parse the HTTP request.
MultipartFormDataRequest mrequest = new MultipartFormDataRequest(request);
khushi paul
Greenhorn

Joined: Feb 08, 2005
Posts: 26
i m using jakarta apache commons fileupload for uploading..
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You'll need to read the API for fileupload.
'getParameter' doesn't work with multipar forms.

This should give you the basic idea.

[ March 14, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
khushi paul
Greenhorn

Joined: Feb 08, 2005
Posts: 26
thanks for ur reply ben,
but as u can see in the code that i've posted,i am using
String field=item.getFieldName();
String value=item.getString();

to get name of field and its value,these functions are from fileupload class only.
i am facing a very strange problem...sometimes i get values and at other time i get null.i am neither able to find out the reason nor solve the problem.

however the file uploading part gets implemented succesfully in any case.
pls help me out,its pretty urgent.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

khushi,
I did miss that in your code, sorry.

I've never seen this problem.
If you have a access to a packet sniffer like tcpflow or Ethereal, it might help to look at what, exactly, is being sent up from the browser.

BTW: If you wrap your code in code tags (there is a code button on the page) your indenting will be preserved, making your code much easier to read. This usually means more people will read it and try to help.
khushi paul
Greenhorn

Joined: Feb 08, 2005
Posts: 26
I'll surely take care of that from next time Ben
BTW my problem got solved i donno how
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: getting null parameters in post
 
Similar Threads
how to reset form bean properties
File uploading and data collection problem
NX: Update method
Action Form Problem
Form Reset