掲示板

How to get HttpServletResponse in Theme

9年前 に Daniel Niklas によって更新されました。

How to get HttpServletResponse in Theme

Junior Member 投稿: 46 参加年月日: 10/11/08 最新の投稿
Hi,

where can i get HttpServletResponse in a theme?
I want to set a cookie within my theme...

Daniel :-)
thumbnail
9年前 に David H Nebinger によって更新されました。

RE: How to get HttpServletResponse in Theme

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
A theme doesn't really have any context where it makes sense to use a cookie. A theme cannot manipulate or change based on a cookie, a theme is not instantiable to do anything based on a cookie, ...

I think there are other more appropriate places to be using cookies.
9年前 に Daniel Niklas によって更新されました。

RE: How to get HttpServletResponse in Theme

Junior Member 投稿: 46 参加年月日: 10/11/08 最新の投稿
hm, in my theme this does make sense ;-)
What do you think, where is a "more appropriate" place?

My current scenario: a simple site with Wecontent Display Portlets. There are no other "custom" portlets. I do not want to set a cookie via javascript, to do this on the server side is imho a more clean solution.
thumbnail
9年前 に David H Nebinger によって更新されました。

RE: How to get HttpServletResponse in Theme

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
Is your theme velocity or freemarker based?
9年前 に Daniel Niklas によって更新されました。

RE: How to get HttpServletResponse in Theme

Junior Member 投稿: 46 参加年月日: 10/11/08 最新の投稿
velocity
thumbnail
9年前 に David H Nebinger によって更新されました。

RE: How to get HttpServletResponse in Theme

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
I cannot find any way within the default context that will allow you to add a cookie. Listing cookies certainly, but not adding a cookie.
9年前 に Daniel Niklas によって更新されました。

RE: How to get HttpServletResponse in Theme

Junior Member 投稿: 46 参加年月日: 10/11/08 最新の投稿
I've solved this with a hook (servlet.service.events.pre). There is already a ticket in JIRA: https://issues.liferay.com/browse/LPS-33675.
thumbnail
9年前 に Olaf Kock によって更新されました。

RE: How to get HttpServletResponse in Theme

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
Daniel Niklas:
I've solved this with a hook (servlet.service.events.pre). There is already a ticket in JIRA: https://issues.liferay.com/browse/LPS-33675.


I fully agree with David: Your theme's velocity code does not have any business in manipulating the response headers. Period. Any code that you have that works with the workaround given in LPS-33675 just does so by accident and can break any time. I've commented the full reason on that ticket, suggesting to close as "won't fix". If you give us the reason why you want to set a cookie, we might be able to point you to a proper location for this operation. So far, velocity is the wrong location.
9年前 に Daniel Niklas によって更新されました。

RE: How to get HttpServletResponse in Theme

Junior Member 投稿: 46 参加年月日: 10/11/08 最新の投稿
ok, perhaps the solution belonging to the velocity template is not perfect. But i don't ask for "velocity"!
I want to set a cookie in my theme!

Any suggestions?
thumbnail
9年前 に Olaf Kock によって更新されました。

RE: How to get HttpServletResponse in Theme

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
Daniel Niklas:
ok, perhaps the solution belonging to the velocity template is not perfect. But i don't ask for "velocity"!
I want to set a cookie in my theme!


Ok, I missed this fine distinction with the ticket. However, the only code that gets executed server side in a theme is in either velocity, freemarker or jsp templates - from that point of view they are equivalent. For technical reasons, because jsps are fully specified with a "request" and "response" object to be available to them, you technically might be able to use it. However, due to the limitations that I outline in that ticket comment, you'll run into the same issues. It might work today, but stop working any time, or under different load situations (when buffers are flushed earlier than your jsp gets executed).

If you want to set cookies from the view layer, generate Javascript that does the job. Accessing the response headers is not guaranteed to work.
9年前 に Daniel Niklas によって更新されました。

RE: How to get HttpServletResponse in Theme

Junior Member 投稿: 46 参加年月日: 10/11/08 最新の投稿
I could do this from within a porltet:
http://docs.liferay.com/portlet-api/2.0/javadocs/javax/portlet/PortletResponse.html#addProperty(javax.servlet.http.Cookie)

... but this after the call in the velocity-template ;-)
thumbnail
9年前 に Olaf Kock によって更新されました。

RE: How to get HttpServletResponse in Theme

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
Could you do this? Of course. Will it work in all cases? No! Will it fail when you least expect it? Who knows.

The view/render phase is the wrong phase to do this, even if you technically have access to the method.

Example: You could redirect (e.g. set 302 header) when rendering. What happens when your portlet is ajaxified, e.g. loaded after the main page has been loaded?

Change state in action phase, render in render phase. If you want to do differently: Go ahead. Just don't come back and complain that you get either an IllegalStateException because the respose has already been committed, or because you miss the headers in a month, even though it worked today.

But I'm repeating myself, I'm out here. Have fun if you want to continue, but you'll need to argue with someone else.
thumbnail
9年前 に David H Nebinger によって更新されました。

RE: How to get HttpServletResponse in Theme

Liferay Legend 投稿: 14919 参加年月日: 06/09/02 最新の投稿
Olaf Kock:
But I'm repeating myself, I'm out here. Have fun if you want to continue, but you'll need to argue with someone else.


You stuck with it longer than me, Olaf. I mean, if you're not going to listen to a "legend" (legend due to years of experience and participating in the forum) and you're not going to listen to an actual Liferay staff member?