Wednesday, 17 September 2014

JSP code fragment for WebSphere Commerce to check if user is logged in or not



The sample code fragment below will set a variable loggedIn to true or false after based on current user register type.

<%
CommandContext commandContext= (CommandContext)request.getAttribute(ECConstants.EC_COMMANDCONTEXT );

Long userId=commandContext.getUserId();

UserDataBean userBean=new UserDataBean();
userBean.setDataBeanKeyMemberId(userId.toString());
userBean.populate();

String userType=userBean.getRegisterType();

if(userType.equalsIgnoreCase(ECConstants.EC_GENERIC_USER_TYPE)){
request.setAttribute("loggedIn",false);
}else{
request.setAttribute("loggedIn",true);
}
%>

<c:if test="${loggedIn}">
    <!-- Do something  -->
</c:if>

Few days later, I discovered it is much easier this way :)

<%
String userType=(String)request.getAttribute("DC_userType");
if(userType.equalsIgnoreCase(ECConstants.EC_GENERIC_USER_TYPE)){
request.setAttribute("loggedIn",false);
}else{
request.setAttribute("loggedIn",true);
}

%>
<c:if test="${loggedIn}">
    <!-- Do something  -->
</c:if>

No comments:

Post a Comment