First Simple : Using JSP code
1) Create the sample Portlet with below code in jsp file
<%@ taglib uri = "http://www.ibm.com/xmlns/prod/websphere/portal/v6.0/portal-navigation" prefix = "portal-navigation" %>
// Put below code in java script
// get var url = portal-navigation:url command="LogoutUser";
var url =
window.location= url ;
Make sure copy the tlds in Web-inf folder.
2) Put that Portlet in page and give it url mapping
3) Send redirect from your servlet code.
Second approach By API
Servlet code
package com.ibm.logoutsimple;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ibm.portal.state.Constants;
import com.ibm.portal.state.StateManager;
import com.ibm.portal.state.accessors.StateAccessor;
import com.ibm.portal.state.accessors.StateAccessorFactory;
import com.ibm.portal.state.accessors.action.engine.logout.LogoutActionAccessorController;
import com.ibm.portal.state.accessors.action.engine.logout.LogoutActionAccessorFactory;
import com.ibm.portal.state.accessors.exceptions.StateNotInRequestException;
import com.ibm.portal.state.accessors.url.URLContext;
import com.ibm.wps.state.accessors.url.EngineUrlEx;
import com.ibm.wps.state.accessors.url.URLAccessorFactoryEx;
import com.ibm.wps.state.accessors.url.URLContextConstants;
import com.ibm.wps.state.utils.StateHelper;
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
getLogOuturl(request, response);
super.doGet(request, response);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doPost(req, resp);
}
private URLContext getURLContext(URLContext urlcontext)
{
return URLContextConstants.FORCE_ABSOLUTE_URLS;
}
private void getLogOuturl( HttpServletRequest httpservletrequest, HttpServletResponse httpservletresponse){
try{
EngineUrlEx engineurlex;
StateManager statemanager = StateHelper.getStateManager();
StateAccessorFactory stateaccessorfactory = (StateAccessorFactory)statemanager.getAccessorFactory(com.ibm.portal.state.accessors.StateAccessorFactory.class);
StateAccessor stateaccessor = stateaccessorfactory.getStateAccessor();
Object obj = null;
try
{
obj = stateaccessor.getStateHolder(httpservletrequest);
System.out.println("1");
}
catch(StateNotInRequestException statenotinrequestexception)
{
System.out.println("2");
obj = statemanager.newState();
}
stateaccessor.dispose();
URLAccessorFactoryEx urlaccessorfactoryex = (URLAccessorFactoryEx)statemanager.getAccessorFactory(com.ibm.wps.state.accessors.url.URLAccessorFactoryEx.class);
engineurlex = urlaccessorfactoryex.newURL(httpservletrequest, httpservletresponse, ((com.ibm.portal.state.StateHolder) (obj)), getURLContext(null), Constants.EMPTY_COPY);
LogoutActionAccessorFactory logoutactionaccessorfactory = (LogoutActionAccessorFactory)statemanager.getAccessorFactory(com.ibm.portal.state.accessors.action.engine.logout.LogoutActionAccessorFactory.class);
LogoutActionAccessorController logoutactionaccessorcontroller = logoutactionaccessorfactory.newLogoutActionController(httpservletrequest, engineurlex.getState());
logoutactionaccessorcontroller.dispose();
String LogOuturl = engineurlex.toString();
// System.out.println(LogOuturl);
String contextpath="PA_LogOutSimple/";
LogOuturl = LogOuturl.replaceAll(contextpath, "");
System.out.println(LogOuturl);
httpservletresponse.sendRedirect(LogOuturl);
}catch (Exception e) {
e.printStackTrace();
}
}
}
Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updating mulesoft Online Training
ReplyDelete