Search code examples
jspservletsurlencodeurl-encoding

How to send query parameters through URL in JSP ( HTTP Status 500:Internal Server Error)


I am trying to send query parameters through this URL from my JSP page to servlet page

<form method="get" action="<%=request.getContextPath()%>/downloadFileServlet?id="<%=rs.getInt("id") %> >

The code in Servlet page to receive those parameters is

int uploadId = Integer.parseInt(request.getParameter("id"));

But I am unable to because the URL as I check in chrome showed

http://localhost:8080/MajorProject/downloadFileServlet?

and what I intend it to show is

http://localhost:8080/MajorProject/downloadFileServlet?id=(whatever the id value is)

Let me know if you need the source code and stack trace.


Solution

  • As the specifications (RFC1866, page 46; HTML 4.x section 17.13.3) state:

    If the method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type.

    So in your situtation you're "violating" specification by specifying url parameter by yourself.


    In order to achieve what you want you can use hidden input fields like this:

    <form action="${context}/downloadFileServlet" method="GET">
      <input type="hidden" name="id" value="<%= rs.getInt("id") %>" /> 
      <input type="submit" /> 
    </form>