Shamik
Shamik

Reputation: 7108

HttpServlet's getParameter("param") return null when there is a value being passed

I am seeing a strange issue where request.getParameter("pg") is returning me null when in the URL I can see its being passed with other parameters. All the other parameters are being printed properly except this one parameter. What could be going wrong?

Map<String,String[]> paramMap = (Map<String,String[]>)request.getParameterMap();
        for (Map.Entry<String, String[]> paramPair : paramMap.entrySet()) {
            LOGGER.info("key - " + paramPair.getKey() + " value =" + paramPair.getValue()[0]);
        }

Sample URL -

http://myserver?searchfor=History+of+Photography&cb=XQ&n=77de88ca&ptnrS=XQyyyyyyYYus&qid=812753692c6536fb529f5ca7ac5aca9b&action=pick&ss=sub&pn=1&st=hp&ptb=447E5AED-F162-40B4-9EE5-A81299D0223D&plp=1&pg=PRshop&redirect=mPWsrdz9heamc8iHEhldEeSc%2FVOw6SzSGaNI5V3YDA%2F2aJhytHNs8w96Bn6LLIZVUIhPwV62vOCzTXhqJvTnW5g66rsz%2FnxYxdLMAYgDRrDL0jeKlXJQ1pFUFFVXHSrvxvGE971vUn5%2F0m7v9vrFJGEoo6nckfQS9o9CGCHUWndDqD0rhA8TW%2Bfa%2Fpxz8JafQQ3ID%2Fsw6GdCvt65%2FomhgEvEX4xv%2B8XQkfNLNvl6%2BpZdZJshoyUfDw5LZBwzFpw52x0Em9ZDVu1sHm4WxLPt9rDUBHj5Wn0eVf5HNoVYGyCa%2FfufPwfQy8OYiLdgvskMPxUTd3YQOJiiO%2F%2ByMdF4Ew%3D%3D&hlc=YWotbWVnYXx4cS1ocA..&pr=PR&ct=GG&tpr=sbt&tp=top

Upvotes: 3

Views: 9144

Answers (1)

DwB
DwB

Reputation: 38300

Per the Java EE 6 API for ServletRequest, getParameter(String) will return null if the parameter does not exist. If you are getting null then the parameter does not exist.

Also, the URL example that you included does not demonstrate this error.

Here is the code I used to test:


public final class BlueServlet extends HttpServlet
{
    private static final long serialVersionUID = 6055351295950796407L;

    private static final Logger logger = LoggerFactory.getLogger(BlueServlet.class);

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        PrintWriter responseWriter;
        @SuppressWarnings("unchecked")
        Map<String, String[]> parameterMap = request.getParameterMap();

        if ((parameterMap != null) && !parameterMap.isEmpty())
        {
            Map.Entry<String, String[]> entry;
            int index;
            Iterator<Map.Entry<String, String[]>> iterator = parameterMap.entrySet().iterator();
            StrBuilder valueBuffer = new StrBuilder();
            String[] values;

            while (iterator.hasNext())
            {
                entry = iterator.next();
                values = entry.getValue();

                valueBuffer.setLength(0);

                if (values != null)
                {
                    for (index = 0; index < values.length; ++index)
                    {
                        valueBuffer.appendSeparator(", ");
                        valueBuffer.append(values[index]);
                    }
                }
                else
                {
                    valueBuffer.append("[none]");
                }

                logger.info("Key: {}; Value(s): {}", entry.getKey(), valueBuffer);
            }
        }
        else
        {
            logger.info("parameterMap is empty.");
        }

        responseWriter = response.getWriter();
        responseWriter.println("<html><head><title>Blue</title></head><body>");
        responseWriter.println("<h1>Blue</h1></body>");
    }

Upvotes: 4

Related Questions