request.getParameter always null

Discussion in 'Java' started by pixelmeow, Jan 19, 2007.

  1. pixelmeow

    pixelmeow Guest

    Hello,

    I'm using the Apache Commons FileUpload package to upload a file,
    along with some other information such as username. I'm using JSP to
    send the file and info to a servlet. The problem is that the
    request's parameters are always null.

    The form section of the JSP:

    <form action="receivefileservlet" method="POST"
    enctype="multipart/form-data">
    <table cellspacing="0" cellpadding="0" border="0"
    width="100%">
    <tr>
    <td valign="middle" align="right" width="20%"
    height="110" style="font-family:Arial;
    font-size:16.0px;"><br />Select a file type to
    upload:&nbsp;&nbsp;</td>
    <td valign="middle" align="left" width="80%"
    height="110" style="font-family:Arial;
    font-size:16.0px;">
    <br />
    <input type="radio" name="inputradio"
    value="0"/>option 1<br />
    <input type="radio" name="inputradio"
    value="1"/>option 2<br />
    <input type="radio" name="inputradio"
    value="2"/>option 3<br />
    <input type="radio" name="inputradio"
    value="3"/>option 4<br />
    <input type="hidden" name="username" value= <%=
    username %> >
    <input type="hidden" name="full_name" value= <%=
    full_name %> >
    <input type="hidden" name="state" value= <%=
    state_code %> >
    </td>
    </tr>
    <tr>
    <td valign="middle" align="right" width="20%"
    style="height:30.0px; font-family:Arial;
    font-size:16.0px;"
    height="37">Enter file name to
    upload:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td valign="middle" align="left" width="80%"
    style="height:30.0px; font-family:Arial;
    font-size:16.0px;"
    height="37">
    <input type="file" name="uploadfield"

    accept="application/excel,application/vnd.ms-excel,application/x-excel,application/x-msexcel"
    size="20"/>
    </td>
    </tr>
    <tr>
    <td valign="middle" align="right" width="20%"
    style="height:30.0px; font-family:Arial;
    font-size:16.0px;">&nbsp;</td>
    <td valign="middle" align="left" width="80%"
    style="height:30.0px; font-family:Arial;
    font-size:16.0px;">
    <input type="submit" name="submit" value="Submit"/>
    </td>
    </tr>
    </table>

    I've tried several ways to get the parameters from this, like:

    --the way I'm used to doing it
    String username = request.getParameter("username").toString();
    String fullname = request.getParameter("full_name").toString();
    String state = request.getParameter("state").toString();
    String filetype = request.getParameter("inputradio").toString();
    --First line throws nullpointerexception

    --(found this on the web while googling)
    Attributes attribs = new Attributes();
    Map map = request.getParameterMap();
    Iterator iter = map.entrySet().iterator();
    while (iter.hasNext()) {
    Entry n = (Entry)iter.next();
    String key = n.getKey().toString();
    String values[] = (String[]) n.getValue();
    attribs.putValue(key,values[0].toString());
    }
    --map values, null, no keys or values

    --The example code from Apache:
    DiskFileItemFactory factory = new DiskFileItemFactory(size, tempPath);
    ServletFileUpload upload = new ServletFileUpload(factory);
    ServletFileUpload sfu = new ServletFileUpload();
    isMultipart = sfu.isMultipartContent(ctx);
    --last line ^^ throws nullpointerexception

    I've been trying to find the answer for this all week, and I can't
    find it. I've got some other code that worked for this, but it
    depended on finding the indexOf() certain characters to get username
    and state, and the file. I don't really want to do that. It was also
    in a JSP rather than a servlet.

    Almost bald from pulling my hair out, I'd love some help, and thank
    you in advance for your consideration.

    --
    Teresa Robinson
    General Dynamics
    Information Technology
    Teresa.Robinson [at] gdit [dot] com
    pixelmeow, Jan 19, 2007
    #1
    1. Advertising

  2. pixelmeow

    Lew Guest

    Why in the world did you set follow-up to alt.fan.heinlein?

    pixelmeow wrote:
    > I'm using the Apache Commons FileUpload package to upload a file,
    > along with some other information such as username. I'm using JSP to
    > send the file and info to a servlet. The problem is that the
    > request's parameters are always null.
    >
    > --The example code from Apache:
    > DiskFileItemFactory factory = new DiskFileItemFactory(size, tempPath);
    > ServletFileUpload upload = new ServletFileUpload(factory);
    > ServletFileUpload sfu = new ServletFileUpload();
    > isMultipart = sfu.isMultipartContent(ctx);
    > --last line ^^ throws nullpointerexception


    You didn't use the factory to create sfu.

    The line didn't throw a "nullpointerexception", it threw a
    "NullPointerException", am I right?

    Which variable was null?

    > I've been trying to find the answer for this all week, and I can't
    > find it. I've got some other code that worked for this, but it
    > depended on finding the indexOf() certain characters to get username
    > and state, and the file. I don't really want to do that. It was also
    > in a JSP rather than a servlet.
    >
    > Almost bald from pulling my hair out, I'd love some help, and thank
    > you in advance for your consideration.


    If your form posted using enctype="multipart/form-data" then you need to use
    org.apache.commons.fileupload.FileItem to retrieve parameters:

    List items = upload.parseRequest( request );
    FileItem item = getItemOfInterest( items, name );
    // use your favorite algorithm to get the item from the List
    String value = (item == null? null : item.getString());

    - Lew
    Lew, Jan 19, 2007
    #2
    1. Advertising

  3. pixelmeow

    pixelmeow Guest

    On Fri, 19 Jan 2007 16:49:41 -0500, Lew <> wrote:

    >Why in the world did you set follow-up to alt.fan.heinlein?


    My goodness, I didn't realize. It's my usual Usenet haunt. I'll have
    to go apologize to everyone there.

    >pixelmeow wrote:
    >> I'm using the Apache Commons FileUpload package to upload a file,
    >> along with some other information such as username. I'm using JSP to
    >> send the file and info to a servlet. The problem is that the
    >> request's parameters are always null.
    >>
    >> --The example code from Apache:
    >> DiskFileItemFactory factory = new DiskFileItemFactory(size, tempPath);
    >> ServletFileUpload upload = new ServletFileUpload(factory);
    >> ServletFileUpload sfu = new ServletFileUpload();
    >> isMultipart = sfu.isMultipartContent(ctx);
    >> --last line ^^ throws nullpointerexception

    >
    >You didn't use the factory to create sfu.


    So the line
    ServletFileUpload sfu = new ServletFileUpload();
    should have had its own factory, another DiskFileItemFactory?


    >The line didn't throw a "nullpointerexception", it threw a
    >"NullPointerException", am I right?


    Yes, I was lazy and didn't capitalize.

    >Which variable was null?


    For the code above, it was ctx, which I had set to null instead of
    RequestContext ctx = new ServletRequestContext(request);
    which I did just now after walking through the code again. I've got
    it setting isMultipart to true. So far so good.

    >> I've been trying to find the answer for this all week, and I can't
    >> find it. I've got some other code that worked for this, but it
    >> depended on finding the indexOf() certain characters to get username
    >> and state, and the file. I don't really want to do that. It was also
    >> in a JSP rather than a servlet.
    >>
    >> Almost bald from pulling my hair out, I'd love some help, and thank
    >> you in advance for your consideration.

    >
    >If your form posted using enctype="multipart/form-data" then you need to use
    >org.apache.commons.fileupload.FileItem to retrieve parameters:
    >
    > List items = upload.parseRequest( request );
    > FileItem item = getItemOfInterest( items, name );
    > // use your favorite algorithm to get the item from the List
    > String value = (item == null? null : item.getString());


    I do have something similar in my code, I just have to work with it a
    bit more. Now that you've helped me get past the above problem, I can
    stop pulling my hair. :)

    Thank you so much, and have a great weekend.

    --
    Teresa Robinson
    General Dynamics
    Information Technology
    Teresa.Robinson [at] gdit [dot] com
    --
    ~teresa~
    AFH Barwench

    =^..^= "Through the walls! The heck with doors!" =^..^=
    Volunteer Coordinator and Database Wrangler
    The Heinlein Centennial, July 7 2007
    http://www.HeinleinCentennial.com
    http://www.forget-me-knotts.com
    email my first name at pixelmeow dot com
    pixelmeow, Jan 19, 2007
    #3
  4. pixelmeow wrote:
    > On Fri, 19 Jan 2007 16:49:41 -0500, Lew <> wrote:
    >
    > >Why in the world did you set follow-up to alt.fan.heinlein?

    >
    > My goodness, I didn't realize. It's my usual Usenet haunt. I'll have
    > to go apologize to everyone there.


    The fact you did it *again* makes me wonder whether
    a) You are doing it deliberately
    b) You do not know how to use your own software
    to post to usenet newsgroups.

    Please fix whatever the problem is, before posting again.

    (alt.test is good for sorting things out)

    (Group address reset to reply to c.l.j.p. - no follow-ups set)

    Andrew T.
    Andrew Thompson, Jan 20, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steveo
    Replies:
    2
    Views:
    5,660
    Michiel Konstapel
    Sep 17, 2003
  2. Rosebud
    Replies:
    4
    Views:
    6,614
    Peter Kirk
    Apr 22, 2004
  3. Teresa Robinson
    Replies:
    2
    Views:
    4,610
    Teresa Robinson
    Jan 31, 2007
  4. param
    Replies:
    0
    Views:
    682
    param
    Aug 3, 2009
  5. paramsethi
    Replies:
    6
    Views:
    6,525
    paramsethi
    Mar 24, 2010
Loading...

Share This Page