留言板

Wrapping non-Struts non-jsp servlets in LifeRay

thumbnail
Pete Helgren,修改在12 年前。

Wrapping non-Struts non-jsp servlets in LifeRay

Regular Member 帖子: 225 加入日期: 11-4-7 最近的帖子
I'll admit that I am very new to liferay and portals but I have been writing servlet applications for years without using struts or jsp's so I find much of the documentation and forum posts to be less than helpful. What I need is a clear understanding of what it takes to:

1. Wrap an existing servlet in a LifeRay portlet without a reference to struts or jsp's..,
OR
2. Take the existing servlet framework I use and modify it so it works with LifeRay portlets.

Option 1 would be preferred because it would reduce the work necessary to implement the servlets I have already written. I use Freemarker as the template engine and use a framework called Niggle to format and deliver those pages in a servlet.

The first thing I tried was just cutting and pasting the existing HTML into the generated .jsp when the portlet was created, but the compiler complained because the Freemarker markup conflicted with the jsp markup. So I just used the htm file as is and got better results: the portlet built and deployed and the HTML rendered just fine but the Freemarker markup wasn't processed. And, since the servlet wasn't referenced none of the Freemarker variables would have been processed anyway since the servlet maps the variables to the actual content.

Seems to me that this can't be too difficult a chore to accomplish, maybe a ServletWrapper class would be nice (although if it were THAT easy it would have already been done!). I haven't read up on Portlets to determine just how it differs from Servlets and frankly since I work with a Servlet framework, I am insulated from the implementation details in any case.

Rendering an HTML page with an iFrame seems kind of hokey if it could be done that way...is there no graceful way to use servlets in a Liferay portlet (reading most of the forum posts on the subject it seems the answer is no....)
thumbnail
Pete Helgren,修改在12 年前。

RE: Wrapping non-Struts non-jsp servlets in LifeRay

Regular Member 帖子: 225 加入日期: 11-4-7 最近的帖子
Ok. Hokey is a bit strong for iFrame integration as I stumbled upon the iFrame Portlet. It works, but can I access other portlet services from my servlet (authentication and authorization come to mind....)? Is there a more elegant way to make a servlet work within LR (going back to the original question)...
thumbnail
David H Nebinger,修改在12 年前。

RE: Wrapping non-Struts non-jsp servlets in LifeRay

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
This is actually not a straightforward thing to do...

You might think as much after comparing the Servlet API to the Portlet API, because they look kinda similar.

The reality of the situation is that there are significant differences between the two...

For example, in the portlet world we don't serve CSS and/or JS directly in the page rendering.

For an even bigger example, in the portlet world we have two main entry points - the action phase and the render phase (an action is triggered to respond to a user action, but only one portlet on a page is acted on, but all portlets on the page must render their content).

You can find plenty of examples of JSP that works just fine in the Servlet world that fails miserably in the Portlet world (because they do some action in the render phase).

What you're asking for is quite the uphill battle, my friend...
thumbnail
Pete Helgren,修改在12 年前。

RE: Wrapping non-Struts non-jsp servlets in LifeRay

Regular Member 帖子: 225 加入日期: 11-4-7 最近的帖子
Thanks David....I was afraid of that...

So, I love the concept of portals and LifeRay is growing on me but my whole Java lifetime has been spent writing web apps as servlets. I have thousands of lines of code and some great applications that would be great as portlets. What is a motivated programmer to do?

Can you point me to an example that compares the two technologies and contrasts them by doing a side by side development of a simple application as a portlet AND servlet so that the differences are clear? Something more than just a trivial "Hello world" application, more on the order of a simple CRUD app? That would go a long way in jump starting me in the portlet development world.

Thanks!
thumbnail
David H Nebinger,修改在12 年前。

RE: Wrapping non-Struts non-jsp servlets in LifeRay

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
Most of the examples that are available start from an existing framework, i.e. Spring Portlet MVC, Vaadin, and of course Liferay's MVC, as well as others. I don't think anyone here actually starts writing portlets directly from the portlet spec, we all tend to leverage one of these frameworks to jumpstart our portlet development.

I don't want to discourage you from moving to Liferay, it's a great platform and, if you start from an existing framework, very easy to get onboard.

For our portlets, we leveraged JSF, Spring, and Hibernate. We did this not to complicate the process, but to eliminate having to write all of the boilerplate code that goes along with even the simplest CRUD applications.

Liferay uses ServiceBuilder to facilitate much of the CRUD stuff, and that is driven by Spring and Hibernate.

I know that adding these new tools to your toolbox can seem overwhelming, but in the long run you'll end up being much more productive...
Pushpinder Singh,修改在12 年前。

RE: Wrapping non-Struts non-jsp servlets in LifeRay

Junior Member 帖子: 84 加入日期: 10-7-21 最近的帖子
Hi Pete,
Main challenge you going to face on custom servlet in liferay is accessing Login User, Roles, ThemeDisplay which are very important objects to work in liferay environment.
You can access Login User and Roles from cookies (Thanks for advise from David H Nebinger).
Not sure how you can access ThemeDisplay object.
thumbnail
Pete Helgren,修改在12 年前。

RE: Wrapping non-Struts non-jsp servlets in LifeRay

Regular Member 帖子: 225 加入日期: 11-4-7 最近的帖子
Accessing the user ID is exactly the problem I am having. Is there a way to accomplish this without the "cookie" approach? The Authentication description given in the IFrame Portlet documentation talks about the Basic Authentication information being (or able to be) passed in the URL. What Parameters carry this information (I didn't see any when I viewed the parameter Map in the request object in my servlet).

Is there some method I can call, or a service I can access from *my* servlet that will return this information?

OPTION#2:

If I create a basic portlet, is there a way to just call my servlet from within the portlet code (no jsp....) I took a look at using the PortletRequestDispatcher but I am not sure I can call a servlet running in the same Tomcat instance but running in a different webapps folder. eg.:

/webapps/myportlet
/webapps/myservlet

Can myportlet call myservlet?
thumbnail
Pete Helgren,修改在12 年前。

RE: Wrapping non-Struts non-jsp servlets in LifeRay

Regular Member 帖子: 225 加入日期: 11-4-7 最近的帖子
And as an FYI. Using the cookie based approach I get the following:

Sep 6, 2011 3:58:03 PM com.liferay.portal.kernel.log.Jdk14LogImpl error
SEVERE: BeanLocator is null
Sep 6, 2011 3:58:03 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet LectureManagerServlet threw exception
com.liferay.portal.kernel.bean.BeanLocatorException: BeanLocator has not been set
at com.liferay.portal.kernel.bean.PortalBeanLocatorUtil.locate(PortalBeanLocatorUtil.java:33)

So it looks like I am at full stop again. Is there no way to solve this?
thumbnail
David H Nebinger,修改在12 年前。

RE: Wrapping non-Struts non-jsp servlets in LifeRay

Liferay Legend 帖子: 14914 加入日期: 06-9-2 最近的帖子
In other threads referencing the beanlocator is null message, it seems to be either a class loader issue or a deployment issue, or possibly connected to Service Builder.

Some folks have been successful doing a tomcat restart after deployment, while others indicate that SB tables should be dropped and allow the SB code to recreate the tables during deployment.

One post indicates it's a known bug that's been fixed in the next release...

Morale of the story seems to be that it is not necessarily your fault. I'd try the restart thing first; that might be a temporary solution that would allow you to continue development and testing, then hope for the fix to be included in the next release.

Sorry I can't be more helpful than that...
thumbnail
Pete Helgren,修改在12 年前。

RE: Wrapping non-Struts non-jsp servlets in LifeRay

Regular Member 帖子: 225 加入日期: 11-4-7 最近的帖子
I saw those posts as well and unfortunately restarting Tomcat failed to solve the issue.

This is a little frustrating (to say the least). The iFrame Portlet would be a great solution to my problem if I could just figure out how to access the portal user ID. I have popped open the source code for the IFrame portlet and it is pretty simple. Probably an easy walk for someone familiar with Struts/JSP (write once, configure everywhere....) but I am getting no joy. I plan to hook up the source to the jars so I can debug the IFrame and see if I can figure out 1. How it works and 2. How I can hack it to append the user ID to my servlet URLor otherwise expose the the userid to the servlet.

It is one of those "Yeah, looks simple I'll have the finished product to you tomorrow" nightmares that has put me days behind schedule. I know I'll crack it and I'll blog on it. I just wish it didn't takes days to sort through it.

Thanks

Pete
thumbnail
Pete Helgren,修改在12 年前。

RE: Wrapping non-Struts non-jsp servlets in LifeRay

Regular Member 帖子: 225 加入日期: 11-4-7 最近的帖子
Take a look at the "Accessing UserID from IFrame" post - http://www.liferay.com/community/forums/-/message_boards/message/10719350 . It solves the wrap a servlet in the portlet problem. Not elegant, but it is working....