3

I'm using jsp of out-of-box portlet like feed.jspf in Liferay 6:

String articleId =null;
HttpServletRequest httpReq = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));
articleId = httpReq.getParameter("articleId");

It is giving a null value whether in custom portlet or in .jsp files, but it should have a value.

Mayur Patel
  • 189
  • 1
  • 6
  • 18
  • Use RenderRequest request = (RenderRequest) FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("javax.portlet.request"); to get the RenderRequest – frandevel Oct 30 '11 at 21:24

6 Answers6

10

Sure, you can always use the standard HttpServletRequest to retrieve your parameters from. You can get this request by using the PortalUtil class, like in the following example:

HttpServletRequest request = PortalUtil.getHttpServletRequest(portletRequest);
String articleId = request.getParameter("articleId");
FoX
  • 234
  • 1
  • 5
  • http://stackoverflow.com/questions/39233585/different-jsp-rendering-in-liferay/39245456#39245456 Could you please look at my thread on the same theme and say if I can use same solution? – Al.Boldyrev Aug 31 '16 at 12:35
8

In my Liferay- JSP I use this:

<!-- ... some imports... -->
<!-- ... some more imports... -->
<%@ page import="com.liferay.portal.util.PortalUtil" %>

<portlet:defineObjects /> <!-- Liferay-specific, defines renderRequest etc.-->

<%
    HttpServletRequest r = PortalUtil.getHttpServletRequest(renderRequest);
    String wellHole =  PortalUtil.getOriginalServletRequest(r).getParameter("well_hole");

%>

(this combines fragments of wisdom from other answers, notably Miguel Gil Martínez's answer from 2012 Jul 23 at 17:35

knb
  • 9,138
  • 4
  • 58
  • 85
2

It worked for me:

public void doView(RenderRequest request, RenderResponse response) throws 
  PortletException, IOException {

HttpServletRequest requestInsideThePortlet = PortalUtil.getHttpServletRequest(request);

String myURLParam =
 PortalUtil.getOriginalServletRequest(requestInsideThePortlet).getParameter("myURLParam");

....
....
....
}
Dungeon Hunter
  • 19,827
  • 13
  • 59
  • 82
2

When working with portlets, each HttpServletRequest paramenter has a prefix which informs the "type" of the parameter and a suffix expressing which instance of the portlet should process it. So, the name of the parameters is not just "articleId". I do not know what portlet are you working but if it was a portlet called, let's say, "example" deployed through a war the parameter would be called example_WAR_exampleportletwar_articleId_w2Xd.

However, you do not have to deal with such complexity. If you are working within a JSP of some already created portlet, there should be an object called renderRequest which contains all parameters and abstracts all this name mangling. So, instead of retrieving the original servlet requestion you an use it:

String articleId = renderRequest.getParamenter("articleId");

If this object is not defined, you just need to insert the <portlet:defineObjects/> tag somewhere and after this the object will be available.

HTH. Let us know if it worked!

brandizzi
  • 26,083
  • 8
  • 103
  • 158
  • ya that is right but i want to get articleId from url ...like http://localhost:8080/author-detail?articleId=12345...can i get that in portlet ..jsp page??? – Mayur Patel Feb 07 '11 at 19:13
  • So, does the URL parameter have no prefix or suffix, right? I do not know how to get it (I'd suggest to do what you tried anyway, hoping it works) but I wonder how this paramenter is set. How is it defined? Is it in some portlet/hook made by you? – brandizzi Feb 07 '11 at 20:44
  • From one of my custom portlet in liferay ...i'm calling other page by defining the name of the page and passing parameters manually...after it will redirect to author-detail page..in that i hv one portlet ..i hv to access articleId in that??.... – Mayur Patel Feb 09 '11 at 04:44
1

For JSF I use this:

@ManagedBean
@RequestScoped
public class OriginalRequest {
    private HttpServletRequest getOriginalRequest() {
        return PortalUtil.getOriginalServletRequest(
                PortalUtil.getHttpServletRequest(
                        (PortletRequest) FacesContext.getCurrentInstance()
                                .getExternalContext().getRequest() ) );
    }

    public String getParam( String name ) {
        return getOriginalRequest().getParameter( name );
    }

    public List<String> getParamNames() {
        return Collections.list( getOriginalRequest().getParameterNames() );
    }
}

Then in your facelet:

#{originalRequest.getParam('my_param')}
Lucas
  • 14,227
  • 9
  • 74
  • 124
0

I tried ur solutions but render request it gives me an exception, so this is another solution:

public String obtainFromUrl(String keyFromWeb) {

    Object outcome = null;
    Map<String, Object> map = FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
    if (map != null) {
        for (String key : map.keySet()) {
            if (map.get(key) instanceof HttpServletRequestWrapper) {
                HttpServletRequest request = (HttpServletRequest) ((HttpServletRequestWrapper) map.get(key))
                        .getRequest();
                outcome = request.getParameter(keyFromWeb);
                break;
            }
        }
    }
    return (String) outcome;

}
cabaji99
  • 1,305
  • 11
  • 9