1

I have deployed my application as a porlet on Liferay, Now I need to perform session management while performing the business logic (in servlet). Let's say I have deployed my servlet on liferay.

I am using liferay 6.2, Tomcat 7, I am submitting one form to servlet before performing the business logic I need to check whether the request is coming from valid user OR Not. So I need to get the login user details for User authentication. How can I get the login user details in servlet? any suggestions?

Vinod
  • 2,263
  • 9
  • 55
  • 104
  • "deployed portlet" and "want to do session management for *servlet*"? I am not sure I understand the question and exactly what you want to do. Please elaborate. And also give the relevant version of Liferay and tomcat. – Prakash K Feb 09 '15 at 08:47
  • I am using liferay 6.2, Tomcat 7, I am submitting one form to servlet before performing the business logic I need to check whether the request is coming from valid user OR Not. So I need to get the login user details for User authentication. How can I get the login user details in servlet – Vinod Feb 09 '15 at 09:20
  • How are you deploying a servlet inside of liferay? – Prakash K Feb 09 '15 at 09:22
  • I have created a web application and created the war for the same. Then I copied the war file in deploy folder of liferay and started the tomcat. So it got deployed. While hitting the servlet URL of that web application. It is working as expected – Vinod Feb 09 '15 at 09:40
  • In that web application, I need to get the currently login user details. Is this possible? If yes How can I do that? – Vinod Feb 09 '15 at 09:41
  • @Prakash,With reference to your answer from the following link, http://stackoverflow.com/questions/970986/accessing-the-user-from-a-liferay-portlet. Is it possible to get the user information in my class/servlet? Where exactly do I need to add the code to get the user information? – Vinod Feb 09 '15 at 10:34
  • you can call 'ThemeDisplay ' object in your portlet class where you are writing your business logic. – Learner Feb 09 '15 at 11:53

2 Answers2

2

You'll have to emulate whatever Liferay does in order to create the whole portlet context. IMHO it's not worth it for the scenario that you're giving. What's the problem of adding your servlet code to a portlet, where you have all of this context?

You can't really deploy a servlet "into Liferay" - your servlet might be running on the same application server as Liferay, but when you look at the HttpServletRequest's targets, you'll see that a servlet request is targetted to your servlet's web application, which is deliberately separated from Liferay - a totally different and unrelated application. When you look at a portlet request, it's directed to Liferay and then internally forwarded to your portlet in another web application. All this is well implemented already and IMHO not worth pursuing. The appserver will resist giving you all the data from another webapplication.

There are other solutions, but they all seem to be prohibitively complex compared to moving the servlet code into a portlet, that I'd wait for more arguments why you definitely need to keep with this implementation before I mention them.

Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
1

You can get logged in user by using following code:

ThemeDisplay td  =(ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

User urs = td.getUser();

Where as it is recommended that, in liferay you should write portlet insted of servlets.

Learner
  • 976
  • 14
  • 29
  • I am little confused now. How should I write in porlet? In any configuration file? Where exactly do I need to add the above code? – Vinod Feb 09 '15 at 11:54
  • If I create any web application and create the .war file and deploy while starting the liferay tomcat. Then will that web application becomes portlet or not? If not How can I create the porlet – Vinod Feb 09 '15 at 11:57
  • You can write this in any of your java class as per your implementation. you just need to import 'import com.liferay.portal.kernel.util.WebKeys;' and 'import com.liferay.portal.theme.ThemeDisplay;' – Learner Feb 09 '15 at 12:01
  • you can refer https://www.liferay.com/documentation/liferay-portal/6.2/development/-/ai/anatomy-of-a-portlet-project-liferay-portal-6-2-dev-guide-03-en – Learner Feb 09 '15 at 12:03
  • Here what does "actionRequest" means, It is showing "actionRequest can not be resolved". Do I need to import anything? – Vinod Feb 09 '15 at 13:38
  • you need to import 'import javax.portlet.ActionRequest; import javax.portlet.ActionResponse;' – Learner Feb 10 '15 at 07:44
  • 1
    This won't work. An actionRequest is a PortletRequest and you don't have that within a servlet. Same with ThemeDisplay: Liferay makes it available in a portletRequest, but a servlet deals with a HttpServletRequest. Liferay doesn't do anything for you in this case - you'll have to use a portlet to get these objects. Not a servlet. – Olaf Kock Feb 10 '15 at 09:23
  • Thank you @OlafKock. I will create the portlet then let you know If I have any queries – Vinod Feb 11 '15 at 05:35