cancel
Showing results for 
Search instead for 
Did you mean: 

How to read request parameters

Former Member
0 Kudos

Hi,

I am trying to port a JSR-168 portlet to a SAP iView that descends from AbstractPortalComponent. So far everything was pretty straightforward, but now I'm stuck on this silly little thing...

Here is an example with the JSR-168 code I am trying to port:

private String exampleMethod(ActionRequest request) 
{
  String queryString = "";
  Enumeration paramNames = req.getParameterNames();
  // add all the request parameters to a query string
  while (paramNames.hasMoreElements()) 
  {
    String name = (String) paramNames.nextElement();
    String[] parameterValues = req.getParameterValues(name);
    for (int j = 0; j < parameterValues.length; j++) 
    {
      String value = parameterValues[j];
      queryString.append(name + '=' + value);
      if (j < parameterValues.length-1) 
        queryString.append('&');
    }
  }
  return queryString;
}

Now from the AbstractPortalComponent descendant I do not have access to the ActionRequest class ofcourse, so I am trying to do the same using the IPortalComponentRequest. I see a method getParameter(String name), but how am I to find out which names are valid? There is no getParameterNames() method....

What am I missing here? How should I find out the names of the parameters (if any) that have been passed to the request?

Kind regards,

-Stijn de Witt

Stijn[DOT]de[DOT]Witt[AT]gx[DOT]nl

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

I am now falling back to using the ServletRequest, like this:

private String exampleMethod(IPortalComponentRequest request) 
{
  HttpServletRequest servletRequest = req.getServletRequest();
  String queryString = "";
  Enumeration paramNames = servletRequest.getParameterNames();

  while (paramNames.hasMoreElements()) 
  {
    String name = (String) paramNames.nextElement();
    String[] parameterValues = servletRequest.getParameterValues(name);
    for (int j = 0; j < parameterValues.length; j++) 
    {
      String value = parameterValues[j];
      queryString.append(name + '=' + value);
      if (j < parameterValues.length-1) 
        queryString.append('&');
    }
  }
  return queryString;
}

Is this the way to do it? I understand that the portal keeps parameters for different iViews separate, but what about if I access the servlet request directly. Will I get parameters meant for other components?

Thanks for any help,

-Stijn

Former Member
0 Kudos

Hi,

once you have the enumeration of parameter names, I would recommend:

while (paramNames.hasMoreElements())

{

String name = (String) paramNames.nextElement();

String value = servletRequest.getParameter(name);

queryString.append(name + '=' + value);

if (j < parameterValues.length-1)

queryString.append('&');

}

Even though I wouldn't build queryString, but something like a Hashtable, with key/value pairs whereas key = name. But maybe you need the complete parameter string concatenated. That depends on your application.

Regards, astrid

Former Member
0 Kudos

Thanks Astrid.

The code you show doesn't actually compile (because of the var 'j' you are still referencing) and my code was just an example. The point is that I want to get the names of all parameters in the request directed at my component. The only way I have found so far is using the ServletRequest, which feels unnatural to me.

Is there another way to get the parameter names without using the ServletRequest? When I access the ServletRequest to get the parameter names, do I get all parameters from the entire page, or only those directed at my component? In JSR-168, the PortletRequest.getParameterNames() will only return the names of parameters directed at the portlet the method was called from, while in the servlet api, the ServletRequest.getParameterNames() returns the names of all parameters for the entire page.

-Stijn

Former Member
0 Kudos

Hi,

I don't know about portlets, but for servlets, jsp's it's not 'unnatural' to query the ServletRequest. That's the way things work in HTTP.

You should get all the parameters that were entered in the submitted form or that were manually added to the call.

Regards, Astrid

Former Member
0 Kudos

Thanks for all your help Astrid. I gave you two times points for 'Helpfull Answer'.

In the SUN portlet spec, JSR-168, you use the PortletRequest.getParameterNames() to get the names of the parameters directed at your portlet. The thing is that if you place the same portlet on a page twice, you would have collisions with the parameter names. In the portlet API, it's assumed that the portal server would prefix the parameters with portlet-specific info. So if you have a parameter named 'search', it would actually become 'portlet1.search' and 'portlet2.search', for example. When you call PortletRequest.getParameterNames() from portlet1, it will return 'search'. When you then call PortletRequest.getParameter('search') it will actually give you the value of 'portlet1.search'. If you would call ServletRequest.getParameter('search') it would return null, you would have to call ServletRequest.getParameter('portlet1.search') to get the value for portlet1. SAP's iViews are like portlets, so I figured that the API would have a similar method.

As I understand it now, it doesn't and you just have to use the ServletRequest.getParameterNames(), same as in a Servlet or JSP. I still wonder how collisions are prevented, but that's another question, for another topic!

Thanks again,

-Stijn