掲示板

An alternative way for Servlet in Liferay hook

12年前 に Feilong Chen によって更新されました。

An alternative way for Servlet in Liferay hook

New Member 投稿: 18 参加年月日: 11/04/20 最新の投稿
I'm creating a customized jsp in a liferay hook; while in the customized jsp I have a form, which I need a servlet while the form is submitted.
I did some research and realized that no servlet is allowed in this situation (correct me if I'm wrong). In the following thread someone suggested using event as an alternative.
http://www.liferay.com/community/forums/-/message_boards/message/7104814

I don't like this approach much and figure there's another alternative: using the customized jsp itself to handle the form, i.e., set action="<%=currentURL%>" with some parameter specifying the form is submitted. I tested this approach and it actually worked.

Is this a good approach? Do somebody has better alternatives?
thumbnail
12年前 に Jonas Yuan によって更新されました。

RE: An alternative way for Servlet in Liferay hook

Liferay Master 投稿: 993 参加年月日: 07/04/27 最新の投稿
Hi Feilong,

Yes, use Servlet-filter and servlet-filter-mappings hooks

Abstracted from the Lifery DEV book:

The portal created a delegation filter to handle all servlet filtering needs. This allows dynamically adding new servlet filters and servlet filters mapping, or overriding existing servlet filters and servlet filters mapping. Current filters and mappings are moved to the XML file liferay-filter-web.xml, read by this filter. Basically this filter takes over the job of servlet container and allows optimizing in addition to adding new filters dynamically.

For instance, to add new servlet filter Knowledge Base Filter, you can leverage servlet filters and servlet filters mapping hooks. First, you could add servlet filters and servlet filters mappings in the liferay-hook.xml as follows.
<hook>
   <servlet-filter>
      <servlet-filter-name>Knowledge Base Filter</servlet-filter-name>
<servlet-filter-impl>com.liferay.knowledgebase.hook.filter.KBFilter</servlet-filter-impl>
      <init-param>
         <param-name>knowledge</param-name>
         <param-value>base</param-value>
      </init-param>
   </servlet-filter>
   <servlet-filter-mapping>
      <servlet-filter-name>Knowledge Base Filter</servlet-filter-name>
      <before-filter>SSO Open SSO Filter</before-filter>
      <url-pattern>/group/*</url-pattern>
      <url-pattern>/user/*</url-pattern>
      <url-pattern>/web/*</url-pattern>
      <url-pattern>*.jsp</url-pattern>
      <dispatcher>REQUEST</dispatcher>
      <dispatcher>FORWARD</dispatcher>
   </servlet-filter-mapping>
</hook>

Then you should provide servlet filter implementation class com.liferay.knowledgebase.hook.filter.KBFilter. Note that the servlet filter class does implement the interface Filter as follows.

public void destroy() {
   // add custom logic
}
public void doFilter(
   ServletRequest servletRequest, ServletResponse servletResponse,
   FilterChain filterChain)
   throws IOException, ServletException {
   String uri = (String)servletRequest.getAttribute(
      WebKeys.INVOKER_FILTER_URI);
   // see details in KBFilter.java
   filterChain.doFilter(servletRequest, servletResponse);
}
public void init(FilterConfig filterConfig) {
   System.out.println(
      "Called KBFilter.init(" + filterConfig + ") where knowledge=" +
   filterConfig.getInitParameter("knowledge"));
   // add custom logic 
}

As you can see, the class KBFilter must implement the methods of the interface Filter: destroy, init and doFilter. Filters perform filtering in the doFilter method. Every Filter like KBFilter has access to a FilterConfig object from which it can obtain its initialization parameters like “knowledge”, a reference to the ServletContext which it can use.

Hope that it helps

Jonas

==================
The Author of Liferay Books:
Liferay User Interface Development
Liferay Portal 6 Enterprise Intranets
Liferay Portal 5.2 Systems Development
Liferay Portal Enterprise Intranets
12年前 に Feilong Chen によって更新されました。

RE: An alternative way for Servlet in Liferay hook

New Member 投稿: 18 参加年月日: 11/04/20 最新の投稿
Thank you Jonas! I didn't know this is possible. I'll try this out. Thanks a bunch!
thumbnail
12年前 に Jonas Yuan によって更新されました。

RE: An alternative way for Servlet in Liferay hook

Liferay Master 投稿: 993 参加年月日: 07/04/27 最新の投稿
Hi Feilong,

note that this feature is available for 6.1.

Thanks

Jonas
12年前 に Tatsyana La によって更新されました。

RE: An alternative way for Servlet in Liferay hook

New Member 投稿: 2 参加年月日: 11/04/04 最新の投稿
Hi Jonas, is it known when the 6.1 will be released? Thanks, Tatsyana
thumbnail
10年前 に Fabian Leonardo Lopez によって更新されました。

RE: An alternative way for Servlet in Liferay hook

New Member 投稿: 12 参加年月日: 13/09/18 最新の投稿
Very helpful information but is it possible to add new servlets by this method or it only allows to filter existing servlets?