Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Banafshe Bamdad
How to call servlet from portlet class?
April 23, 2008 5:18 AM
Answer

Banafshe Bamdad

Rank: Junior Member

Posts: 56

Join Date: November 6, 2007

Recent Posts

Hi,
I have wrote a portlet and put its war file in the webapps tomcat. I wrote a servlet class to download files But, I don't know how to call servlet from portlet class. Also. using of PortletDispatcher is not effective. The following is portlet web.xml
<?xml version="1.0"?>

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">

<display-name>UserArticles</display-name>
<context-param>
<param-name>company_id</param-name>
<param-value>*</param-value>
</context-param>
<listener>
<listener-class>com.liferay.portal.kernel.servlet.PortletContextListener</listener-class>
</listener>
<listener>
<listener-class>
org.apache.commons.fileupload.servlet.FileCleanerCleanup
</listener-class>
</listener>
<servlet>
<servlet-name>UserArticles</servlet-name>
<servlet-class>com.liferay.portal.kernel.servlet.PortletServlet</servlet-class>
<init-param>
<param-name>portlet-class</param-name>
<param-value>com.ics.portlet.UserArticles.UserArticlesPortlet</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>GetArticleFileServlet</servlet-name>
<servlet-class>com.ics.portlet.UserArticles.GetArticleFileServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>GetArticleFileServlet</servlet-name>
<url-pattern>/UserArticles/GetArticleFileServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>UserArticles</servlet-name>
<url-pattern>/UserArticles/*</url-pattern>
</servlet-mapping>
</web-app>

and I used PortletDispatcher accordingly.

HttpServletRequest httpServletRequest = PortalUtil.getHttpServletRequest(renderRequest);

HttpServletResponse httpServletResponse = PortalUtil.getHttpServletResponse(renderResponse);
String portletId = themeDisplay.getPortletDisplay().getId();
PortletContextWrapper pcw = PortletContextPool.get(portletId);
ServletContext servletContext = pcw.getServletContext();


RequestDispatcher rd = servletContext.getRequestDispatcher("/GetArticleFileServlet");
try {
rd.include(httpServletRequest,httpServletResponse);
} catch (ServletException e) {
e.printStackTrace();
}

Please Help me.
Thanks in advance
Victor Zorin
RE: How to call servlet from portlet class?
April 23, 2008 10:52 PM
Answer

Victor Zorin

Rank: Liferay Legend

Posts: 1178

Join Date: April 14, 2008

Recent Posts

just a personal view: I think it is rather a tricky thing to hook servlets and portlet actions in single render/action line of fire. Would not it be better to implement file uploading within the portlet framework? It is the same effort.
Banafshe Bamdad
RE: How to call servlet from portlet class?
May 30, 2008 1:18 AM
Answer

Banafshe Bamdad

Rank: Junior Member

Posts: 56

Join Date: November 6, 2007

Recent Posts

To begin, I need to develop a servlet which will downlod files.

package com.ics.portlet.JournalManagement;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ics.portlet.JournalManagement.model.PaperFilesBean;

public class DownloadServlet extends HttpServlet {

private static final long serialVersionUID = 5472703721421222620L;

@SuppressWarnings("unchecked")
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession session = req.getSession();
ArrayList<PaperFilesBean> paperFilesBean = (ArrayList<PaperFilesBean>) session.getAttribute("array_list_of_file");
String fileIdStr = req.getParameter("file_ID");
int fileId = Integer.parseInt(fileIdStr);
PaperFilesBean paperFile = paperFilesBean.get(fileId);
String fileName = paperFile.getFileName();
String _uploadDir = paperFile.getFilepath();
if (!res.isCommitted()) {
if (fileName != null) {
File file = new File(_uploadDir + "/" +fileName);
if (file.isFile()) {
byte[] bytes = new byte[(int) file.length()];
ServletOutputStream out = res.getOutputStream();
res.setContentType("application/pdf");

res.setHeader("Content-Disposition", "attachment; filename=" + fileName);
res.setHeader("Content-Length", String.valueOf(file.length()));

try {
FileInputStream fis = new FileInputStream(file);
fis.read(bytes);
fis.close();
} catch (FileNotFoundException e) {
bytes = "Error.FileNotFound".getBytes();
return;
} catch (IOException e) {
e.printStackTrace();
bytes = "Error.IOException ".getBytes();
return;
}
out.write(bytes);
out.flush();
}
}
}
}
}




Servlet gets the file information from the session. Then I insert the following line to portlet class.

ArrayList<PaperFilesBean> paperFilesBean = PersistenceManager.getPaperFilesBeanById(paperId);
portletSession.setAttribute(ARRAY_LIST_OF_FILE, paperFilesBean, PortletSession.APPLICATION_SCOPE);



Then I define <servlet> and <servlet-mapping> tags in the web.xml file


<servlet>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>com.ics.portlet.JournalManagement.DownloadServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/download</url-pattern>
</servlet-mapping>



I define jamon file to be the view my portlet and use <a> tag to download file.

<a href="/JournalManagement/download?file_ID=<% fileIndex %>"><% JamonContext.getMean("download_file", languageHashMap) %></a>



finally, I set emptySessionPath="true" in tomcat/conf/server.xml
Jeffrey Blattman
RE: How to call servlet from portlet class?
June 2, 2008 3:18 PM
Answer

Jeffrey Blattman

Rank: New Member

Posts: 21

Join Date: October 24, 2007

Recent Posts

you can't serve a content type other that of the portal from a portlet. you can imagine why. a page can only have one content type, so the portal allow each portal to set the content type.

i'm also confused because i see the link you have is directly to the servlet, but the code snippet shows you calling PortletDispatcher.include(). how is the portlet getting invoked if the link is directly to the servlet?

regardless, what you want to do is to use the JSR-286 serveResource() method. see the specspec for more details.
Banafshe Bamdad
RE: How to call servlet from portlet class?
June 7, 2008 11:03 PM
Answer

Banafshe Bamdad

Rank: Junior Member

Posts: 56

Join Date: November 6, 2007

Recent Posts

At first, I used PortletDispatcher to call servlet, but I found that it is not possible. Therefore, I used a link that it directly redirect to servlet.