Different "request" implicit objects in Liferay

attributes, java, liferay, request, servlets

Solution

What is the correct approach for accessing request attributes? In portlets, the correct approach is to only interact with the `renderRequest` for retrieving parameter values and for getting or setting request attributes (in JSPs or the portlet class). `renderResponse` can be used to create new Portlet URLs.

Why can you get request attributes from the `request` object as well? `request` is an `HttpServletRequest` and `renderRequest` is a `PortletRequest`. However, Liferay implemented `request` as a wrapper of `HttpServletRequest` in such way that, e.g. for accessing request attributes, it will fallback to the `PortletRequest` if it doesn't find the attribute in the actual `HttpServletRequest`.

What's the use of `actionRequest` and `actionResponse` at view time? Like you say, if you follow the principles of MVC, you will only use the JSP for view logic. If you check the `DefineObjectsTag` from Liferay, you can see that all these `xxxRequest` and `xxxResponse` objects are only set if the portlet is in the right lifecycle. Because, normally, you're in the RENDER_PHASE when executing the JSP logic, only `renderRequest` and `renderResponse` will be not-null.

When is it useful to store an attribute in the `request`? It doesn't really make sense to store attributes in the `HttpServletRequest` if you're working with portlets. On the other hand, inside a servlet (filter) you could add attributes that can then be retrieved from portlets by using `request.getAttribute("xxx")`.

Problem

What is the difference between fetching attributes from these implicit objects: ``` renderRequest.getAttribute("myVar") actionRequest.getAttribute("myVar") request.getAttribute("myVar") ``` Why are they all allowed? I mean you usually store attribute in `actionRequest` or `renderRequest` object but you can get it in `request` implicit object, why? What is the correct approach? How is it possible to get an action object in view time? Does not it violate the action-render renderParams passing mechanism? Why are actionRequest/response available as implicit object if they throw `NullPointerException` when trying to use them in JSP? Finally when is it useful to store an attribute in the request (`PortalUtil.getOriginalServletRequest`)?

Original source