How to share data between web-apps

How to share data between web-apps

way 1) Sharing data using cookies

===========================

Code on WebApp1 Login Servlet

———————————————-

 below code will be execute once you click on any link or button , and it will call the login servlet  

 of webApp2 and webApp2 will read the data from the cookies


userData.put(“password”, radiusLoginForm.getPassword());

Cookie cookie1 = new Cookie(“username”,”admin”);

cookie1.setPath(“/”);

Cookie cookie2 = new Cookie(“password”,”admin”);

cookie2.setPath(“/”);

response.addCookie(cookie1);

response.addCookie(cookie2);

code on webapp2 Login Servlet to obtain data from the cookies

—————————————————————————————–

String username= “”;

String password= “”;

Cookie cookies[] = request.getCookies();

for(Cookie cookieObj : cookies){

if(cookieObj.getName().equalsIgnoreCase(“username”)){

username = cookieObj.getValue();

}else if(cookieObj.getName().equalsIgnoreCase(“password”)){

password = cookieObj.getValue();

}

}

way 2) Sharing data using ServletContext

=====================================

Note : Lets assume that we are accessing webApp2 from webApp1

First open and edit the tomcat “server.xml”

Find the tag into server.xml and add the crossContext=”true” attribute into both the

context tag

Ex: Then add the context param into the web.xml of  webApp2

Ex:   SharedSessiondataContext

/webApp1

Code into the webApp1 Login servlet (this servlet you will call from the webApp1 servlet)

—————————————————————————————————————–

below code will obtained you the data which you have shared using the webApp1 servlet context

HashMap userData = new HashMap();

userData.put(“username”, “admin”);

userData.put(“password”, “admin”);

storeDataInContext(getServlet().getServletContext(), sessionId, userData);

public synchronized static void storeDataInContext(ServletContext context, String sessionid, HashMap
String> userData)
{
Hashtable> shareddata = new Hashtable>();
shareddata.put(sessionid, userData);
context.setAttribute(“sharedUserData”, shareddata);
}

Code into webApp2 Login Servlet
———————————————–
HashMap userData = getUserRolesFromContext(getServletConfig().getServletContext(),ssoSessionid);

public static HashMap getUserRolesFromContext(ServletContext context, String ssosessionid) {
String SignonContext = context.getInitParameter(“SharedSessiondataContext”);
HashMap userData = null;
if(SignonContext!=null){
ServletContext ssocontext = context.getContext(SignonContext);
if(ssocontext!=null){
Hashtable> shareddata = ((Hashtable
HashMap>)ssocontext.getAttribute(“sharedUserData”));
if (shareddata!=null) {
Logger.logInfo(MODULE,”SSOSESSIONID : “+ssosessionid);
userData =(HashMap)shareddata.get(ssosessionid);
}
}
}
return userData;
}

way 3) Sharing Data using URL parameters

=====================================

This code is for basic sso implementation using URL parameters

WebApp1 

————-

Lets assume that you are logged-in in your WebApp1 and

calling below URL of WebApp2 using any menu-link in WebApp1 jsp page

i.e : http://localhost:8080/WebApp2/LoginServlet?sessionId=<%=request.getSession().getId()%>

once you will clicked upon this url your WebApp2’s LoginServlet will get called and below code will be

executein your LoginServlet of WebApp2

code in LoginServlet of WebApp2:

================================

String sessionIdParam = request.getParameter(“sessionId”);

String userIdParam = request.getParameter(“userId”);

if(sessionIdParam!=null && userIdParam==null){

counter = 0;

tempSessionId = sessionIdParam;

String ssoUrl = getSSOUrl(); // this method will

if(ssoUrl!=null){

Logger.logInfo(MODULE, “Forwarding to “+ssoUrl);

response.sendRedirect(“http://localhost:8080/WebApp1/LoginServlet?

sessionId=”+sessionIdParam);

// from here LoginServlet of WebApp1 will be called and we will send back the same sessionId

}else{

System.out.println(“Invalid URL”);

}

return;

}else{

counter++;

}

if(sessionIdParam!=null && userIdParam!=null){

if(tempSessionId.equals(sessionIdParam) && counter==1){

// you are done with the user verification you can do here further code now

}

}

code in LoginServlet of WebApp1:

================================

String sessionId = (String)request.getParameter(“sessionId”);

// userId/username you need to set in session after logged into the WebApp1, so you will get it from session

as given below

String userId = (String)request.getSession().getAttribute(“userId”);

String currentSessionId = request.getSession().getId();

if(sessionId!=null && userId!=null){

if(currentSessionId.equals(sessionId)){

response.sendRedirect(“http://localhost:8081/WebApp2/LoginServlet?userId=”+userId

+”&sessionId=”+sessionId);

}

}

}

Read Me:

——–

In Above code we are verifying the sessionid with the sessionid we have received from the WebApp2’s

response.sendRedirect() url and after verifying the url we are sending back the same sessionId , and this time

we are sending the userId also, so now we will have sessionId and userId in the WebApp2, so we will verify this sessionId again on the WebApp2 and check whether we have received the userId or not, and if the sessionId is not null and userId is not null then we will verify the sessionId with the tempSessionId and then its done…

Advertisements

About rajkirpal

i am a software engineer. and i have decent knowledge about java technologies like ANDROID, Servlet, Jsp, Struts, Hibernet, Spring, J2ME, Java Script, Ajax, JQuery, CSS. Further i am Preparing Easy understandable materials/documents upon the same java technologies and developing project in core java , advanced java and j2me , ANDROID as per the requirements Also designing the web sites and logos Thank You.
This entry was posted in J2EE. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s