1

I have a portlet developed in Liferay platform in which I have added the logic to get the query parameter value from URL. I have got the site: http://localhost:8080/web/guest/example, Now this site is being called from the another external site that is not in Liferay with query parameter at end: http://localhost:8080/web/guest/example?value=test. In Liferay Portlet code I have applied the logic to get the Parameter value from the URL which is not working. It returns the "null" value:

HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(request)); 
        String myValue = httpReq.getParameter("value");     
        System.out.println(myValue);

I tried this way too but get the same "null" value from Query parameter:

HttpServletRequest httpRequest = PortalUtil.getHttpServletRequest(request);
        String myValue = httpRequest.getParameter("value");
        System.out.println(myValue);

Any suggestion what I am doing wrong here or how can I get the query parameter value coming from external site?

TechPro
  • 331
  • 1
  • 10
  • 29

2 Answers2

1

If you have happened to set

    <render-weight>0</render-weight>
    <ajaxable>true</ajaxable>

in liferay-portlet.xml, the portlet would be rendered through Ajax and no longer in the same HTTP-Request. I've tried it: Without these settings your code (the first alternative) ran well (in the doView method).

However, it's bad practice to rely on access to random request parameters (in a portal) anyway... You should rather construct a full portal URL or use the friendly-URL features for Liferay. That way you're really in the portal world and not in a random servlet/portlet mix.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
  • Hi Olaf Kock, I checked my liferay-portlet.xml in local, I don't have that settings related to render-weight and ajaxable but still I am getting null value for parameter. There is going to be several values as query parameter from the external site which refers to the same Portlet in Liferay, and those values that are passed through query parameter must be inserted in database. If I need to create friendly-URLdifferent values like putting ?value=test at end of URL Liferay doesn't accept the query parameter at end as friendly URL. – TechPro Jan 21 '16 at 18:21
  • you might want to post more code, because the code you post above works for me. – Olaf Kock Jan 21 '16 at 20:15
  • Olaf Kock, I have form portlet, when user enters details and submits, it enters data into database. In view.jsp I have the fields to enter the names and details like FisrtName.... Then there is a call to Java class function to insert the entered data to database. To test the query parameter, I have not supplied any value in code, just using the above code to get query parameter from URL. In local testing I am just supplying the query parameter as: http://localhost:8080/web/guest/example?value=test and filling the form and submitting. The value I am getting as query parameter is null. – TechPro Jan 28 '16 at 19:58
1

I just tried the following inside doView() and it works for me:

HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(renderRequest);
HttpServletRequest httpOrigReq = PortalUtil.getOriginalServletRequest(httpReq);
String myValue = httpOrigReq.getParameter("value");

The only difference (may be) is that I used the RenderRequest-object. (As I don't see the type of your request-object.)

Thorsten Laux
  • 100
  • 12