掲示板

Obtain ServletContext or PortletContext in portlet

12年前 に Seyed Mohammad Hosein Jamali によって更新されました。

Obtain ServletContext or PortletContext in portlet

Junior Member 投稿: 77 参加年月日: 09/12/13 最新の投稿
Hello All

I want to use below code in a portlet that created by Liferay IDE to obtain ServletCotext.

ServletContext sc = ((PortletContextImpl)req.getPortletSession().getPortletContext()).getServletContext();

where req is of type RenderRequest , but i get this exception :
com.liferay.portlet.PortletContextImpl cannot be cast to org.apache.pluto.core.impl.PortletContextImpl

Definition of later PortletContextImpl is :
public class org.apache.pluto.core.impl.PortletContextImpl implements javax.portlet.PortletContext, org.apache.pluto.core.InternalPortletContext {


How can i use of com.liferay.portlet.PortletContextImpl without using portal-impl.jar ?
Is there a better way to obtain ServletContext ?

Cheers ,
S.M.H. Jamali
thumbnail
12年前 に Corné Aussems によって更新されました。

RE: Obtain ServletContext or PortletContext in portlet

Liferay Legend 投稿: 1313 参加年月日: 06/10/03 最新の投稿
I would try to cast this not to org.apache.pluto.core.impl.PortletContextImpl but to javax.portlet.PortletContext;
12年前 に Seyed Mohammad Hosein Jamali によって更新されました。

RE: Obtain ServletContext or PortletContext in portlet

Junior Member 投稿: 77 参加年月日: 09/12/13 最新の投稿
of course i can cast to javax.portlet.PortletContext too , but i need to cast to PortletContextImpl ! as i need call getServletContext() of PortletContextImpl !
I need to access the ServletContext of portlet

Any help appreciated
thumbnail
12年前 に Corné Aussems によって更新されました。

RE: Obtain ServletContext or PortletContext in portlet

Liferay Legend 投稿: 1313 参加年月日: 06/10/03 最新の投稿
Liferay ain't Pluto ;

org.apache.pluto.core.impl.PortletContextImpl
12年前 に Seyed Mohammad Hosein Jamali によって更新されました。

RE: Obtain ServletContext or PortletContext in portlet

Junior Member 投稿: 77 参加年月日: 09/12/13 最新の投稿
Corné Aussems:
Liferay ain't Pluto ;

org.apache.pluto.core.impl.PortletContextImpl



Yes i know , Thank you.
thumbnail
12年前 に tinu c p によって更新されました。

RE: Obtain ServletContext or PortletContext in portlet

Junior Member 投稿: 78 参加年月日: 10/01/07 最新の投稿
Hi Seyed,

Try using below code

String portletId = "your-portlet-id";
com.liferay.portal.kernel.portlet.PortletBag portletBag = com.liferay.portal.kernel.portlet.PortletBagPool.get(portletId);
ServletContext servletContext = portletBag.getServletContext();

Hope it helps
12年前 に Seyed Mohammad Hosein Jamali によって更新されました。

RE: Obtain ServletContext or PortletContext in portlet

Junior Member 投稿: 77 参加年月日: 09/12/13 最新の投稿
tinu c p:
Hi Seyed,

Try using below code

String portletId = "your-portlet-id";
com.liferay.portal.kernel.portlet.PortletBag portletBag = com.liferay.portal.kernel.portlet.PortletBagPool.get(portletId);
ServletContext servletContext = portletBag.getServletContext();

Hope it helps



Hi tinu c p
Thats works , Thank you.

for get portletid i use of this :


   ThemeDisplay themeDisplay= (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
   PortletDisplay portletDisplay= themeDisplay.getPortletDisplay();
   String portletId= portletDisplay.getId();


Thanks
S.M.H.Jamali
thumbnail
12年前 に tinu c p によって更新されました。

RE: Obtain ServletContext or PortletContext in portlet

Junior Member 投稿: 78 参加年月日: 10/01/07 最新の投稿
Glad it helped u seyed.

Thanks,
AP
12年前 に Seyed Mohammad Hosein Jamali によって更新されました。

RE: Obtain ServletContext or PortletContext in portlet

Junior Member 投稿: 77 参加年月日: 09/12/13 最新の投稿
I find out a simple way for obtaining the ServletContext :


HttpServletRequest request = PortalUtil.getHttpServletRequest(req);
ServletContext servletContext = request.getSession().getServletContext();


req is of type RenderRequest

Thats it emoticon
9年前 に viral bhat によって更新されました。

RE: Obtain ServletContext or PortletContext in portlet

New Member 投稿: 5 参加年月日: 14/05/05 最新の投稿
Thanks. That worked for me.