2

I'm trying to pass a URL parameter from a JSP to a Sevlet. My URL is

/portal/faces/student.jsp?owner_id=1030303i

In my JSP i added this

<form action="steg" method="post" enctype="multipart/form-data">
     <input type='text' value='<%=request.getParameter("owner_id")%>' id="owner"/>
     <input type="file" name="file" size="50"  />
            <br />             
            <input type="submit" value="Steganograph" />
        </form>

However, in the servlet i did this,

              String owner = request.getParameter("owner");
              System.out.println("aaaaaaaaaaaaaaaa"+owner);
              response.sendRedirect("stegsuccess.jsp?owner_id="+owner);

the result that is printed out is null. Is something wrong there? Anyone that knows how to fix this? Please advice. Thanks :)

user1681961
  • 100
  • 1
  • 19
user1651129
  • 25
  • 2
  • 6

3 Answers3

1

There are 2 problems in your code:

  1. You didn't give the input field a name. You expected that the id attribute of the input element is been used as request parameter name. But this is not true. It's the name attribute.

    <input type="text" name="owner" value="${fn:escapeXml(param.owner_id)}" />
    

    (note: I've taken the liberty to seal a XSS attack hole in the value attribute for you)

  2. You're using multipart/form-data encoding and yet expecting that the request parameters are available by HttpServletRequest#getParameter(). This is by default not supported. This will only work if you're using a servlet with @MultipartConfig on a Servlet 3.0 compatible container. Or maybe you're working on an existing project and someone else has already invented a servlet filter which transparently parses multipart/form-data requests and fills the request parameter map before passing the request to the servlet. In any case, make sure that you understand how to parse multipart/form-data requests: How to upload files to server using JSP/Servlet?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

What version of servlets are you using?

multipart/form-data encoded requests are indeed not by default supported by the Servlet API prior to version 3.0. The Servlet API parses the parameters by default using application/x-www-form-urlencoded encoding. When using a different encoding, the request.getParameter() calls will all return null. When you're already on Servlet 3.0 (Glassfish 3, Tomcat 7, etc), then you can use HttpServletRequest#getParts() instead.

From https://stackoverflow.com/a/3337115/139010. See also Issue with Servlet Multipart request.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • I'm not really too sure what's the version I'm using. I tried using the links you've given me. But it seems that its not working with getValues and returning me to a wrong page. Any idea if there is other method for this? – user1651129 Sep 11 '12 at 13:47
  • Look in web.xml. The top-level element is `` and its `version` and `xsi:schemaLocation` attributes are a hint as to what servlet version you're using. – Matt Ball Sep 11 '12 at 14:27
0

sendRedirect() method wont carry the request object forward. Instead use RequestDispatcher rd = request.getRequestDispatcher("yourpage.jsp");rd.forward(req, res) method which forwards the request object and so the request parameters.

Ratnakar.class
  • 301
  • 5
  • 12
  • This is correct from Servlet code of user. If you look at JSP code,the request is `enctype="multipart/form-data"`.So, `request.getParameter("paramName");` will return NULL. – Hardik Mishra Sep 12 '12 at 04:38