Ram
Ram

Reputation: 117

Using the same session for multiple applications

I have developed a struts2 application with login feature. My java code for this is

        Map session = ActionContext.getContext().getSession();
        session.put("login", "true");
        session.put("user", username);

and I will check if the user already logged in jsp as

<s:if test="#session.login != 'true'"> 

Now I want to develop another application. As the users will be same I do not want to ask them to login again.

I want to access the session of application1 in application2. How to do this. Please help.

Upvotes: 0

Views: 1673

Answers (2)

Maas
Maas

Reputation: 1357

If your applications are hosted on the same tomcat instance, you can use cross context feature.

In server.xml add the following line

<Context path="/<path>" crossContext="true">

Then you can access the context shared by application like this:

getContext("yourApp").getAttribute("AttrName");

There are few similar questions...

What does the crossContext attribute do in Tomcat? Does it enable session sharing?

Sharing Session object between different web applications

Upvotes: 1

NickJ
NickJ

Reputation: 9579

Struts2 session tracking uses the underlying Servlet session tracking system. You should get some understanding of how this works, given that HTTP is stateless. There's an explanation here.

This underlying mechanism (using cookies or URL re-writing) itself only works on a single web-application, so you cannot use it directly.

Instead, I suggest using your own cookie, and have both applications able to read the cookie (to check if the user is logged in) and set it (when logging in). Set the value of the cookie to some unique ID of the logged-in user.

Upvotes: 1

Related Questions