Powered By Blogger

Monday, May 23, 2011

Adding servlet in Liferay portlet

In the Web.xml you should have the below code mapping



servlettest-portlet

index.html
index.htm
index.jsp
default.html
default.htm
default.jsp



testServlet
com.liferay.portal.kernel.servlet.PortalDelegateServlet

servlet-class
com.aonehewit.servlet.TestServlet


sub-context
testServlet

0





testServlet
/testServlet/*





http://java.sun.com/portlet_2_0

/WEB-INF/tld/liferay-portlet.tld






In the class you should have the below code

/**
*
*/
package com.aonehewit.servlet;

import com.liferay.portal.kernel.servlet.PortalDelegatorServlet;
import com.liferay.portal.kernel.util.JavaConstants;
import com.liferay.portal.kernel.util.WebKeys;

import java.io.IOException;

import javax.portlet.PortletRequest;
import javax.portlet.PortletResponse;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author basanagowda.patil
*
*/
public class TestServlet extends HttpServlet {




@Override
public void init() throws ServletException {
// TODO Auto-generated method stub
super.init();
System.out.println("Inside the init() method****************8 of TestServlet");
}




@Override
protected void service(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
System.out.println("Inside the service******************TestServlet");
super.service(arg0, arg1);
}




@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("Inside the doGet method**********************888");
super.doGet(req, resp);
}



@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);

System.out.println("Inside the Do post me thod:::::::::::::::::::");
}


}


The url to access the servlet will be as below

http://localhost:8080/delegate/testServlet

Note : delegate is not a portlet it is another servlet which is used to invoke our servlet.

2 comments: