2
votes

I come back with the same question but this time more documented. i have a web application with many servlets and JSPs. The application has a LogIn option. In the LogIn servlet i start a new session, and after placing some informations in the session i go forward to a JSP.

LogIn.java relevant code:

HttpSession sess = request.getSession(true);
sess.setAttribute("GLN", user);
rd.forward(request, response);

After I forward, I get a Jsp page called Insert.jsp where I get the sessions attributes.

Insert.jsp relevant code:

HttpSession sess = request.getSession(false);
if (sess != null){
out.println(sess.getAttribute("GLN"));
}

After this i have a form that directs me to a servlet Adaugare.java. Here i do the same thing:

Adaugare.java code:

HttpSession sess = request.getSession(false);

Here comes the problem. Later edit: This returns null, as no session exists. Then i forward to same Insert.jsp file and there, even if i have HttpSession sess = request.getSession(false);, a new session with a new session ID is created different from the first one. So obviously out.println(sess.getAttribute("GLN")); returns null.

This is the long story. The short version:

When i go from a servlet to a jsp, session is ok, when i go from a jsp to a servlet, session is nowhere to be found . Then a new session is created when i forward to a JSP. Practically it creates a new cookie. If i print the contextPath from JSP and serlet, it's the same.

But here is the strange thing. This happens when i run the application on a apache with a mod_jk. When i run the app from a tomcat, it works fine.......

Please help, i've been stuck for 2 weeks on this problem.

Answer to dan: (Text to long for comment and need to wait 7 hours to reply my own question) I deleted all comented lines. Hope that is ok. I'm not the one in charge with the server, but the one who is told me it's not multiple workers.

worker.list=jk-status
worker.jk-status.type=status
worker.jk-status.read_only=true

worker.list=jk-manager
worker.jk-manager.type=status

worker.list=balancer
worker.balancer.type=lb

worker.balancer.error_escalation_time=0

worker.balancer.max_reply_timeouts=10

worker.balancer.balance_workers=node1
worker.node1.reference=worker.template
worker.node1.host=localhost
worker.node1.port=8109

worker.node1.activation=A

worker.balancer.balance_workers=node2
worker.node2.reference=worker.template
worker.node2.host=localhost
worker.node2.port=8209

worker.node2.activation=A

worker.template.type=ajp13

worker.template.socket_connect_timeout=5000

worker.template.socket_keepalive=true

worker.template.ping_mode=A

worker.template.ping_timeout=10000

worker.template.connection_pool_minsize=0

worker.template.connection_pool_timeout=600

worker.template.reply_timeout=300000

worker.template.recovery_options=3
1
Are you using multiple workers, behind mod_jk? - dan
No multiple workers. I bet it's something from the cookies creation, but i can't find what. - Ionut Zeca
HTTPS in Login and not otherwise? - TJ-
Sorry T J, i don't understand the question...The code is copy-paste from files. - Ionut Zeca

1 Answers

1
votes

If the requests are balanced between multiple workers you should set the session stickiness flag to true. See: http://tomcat.apache.org/connectors-doc/reference/workers.html for more details. You should try:

worker.balancer.sticky_session=True