I am developing a webapp in java(using the webwork framework). When i am accessing the server directly i.e. , the session management works OK. If i configure a domain , i.e. which maps to an apache server which forwards the requestes to the internal server it always registers a new session with the server. (so the user is essentially logged in for 1 page, and then is seen as logged out for everypage thereafter)
For the session management i have a session listener and a session service.
The listener is configured inside web.xml as attached.
Attached also SessionService, SessionListener and Tomcat out
I would like any insights for solving this problem, or any solutions for replacing this code with something else that would work.
Thanx
C.
<listener>
<listener-class>cy.dating.utils.SessionListener</listener-class>
</listener>
SessionListener:
------------------------
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;
public class SessionListener implements HttpSessionListener{
public void sessionCreated(HttpSessionEvent event) {
SessionService.getInstance().addSession(event.getSession());
}
public void sessionDestroyed(HttpSessionEvent event) {
SessionService.getInstance().removeSession(event.getSession());
}
}
SessionService:
-----------------------
import ....domainobjects.Registration;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.log4j.Category;
public class SessionService {
private static SessionService ourInstance = new SessionService();
private HashMap sessions = new HashMap();
private static Category logger = Category.getInstance(SessionService.class);
public static SessionService getInstance() {
return ourInstance;
}
private SessionService() {
}
public void addSession(HttpSession ses){
sessions.put(ses.getId(), ses);
logger.debug("Registered new session: "+ses.getId());
}
public void removeSession(HttpSession ses){
sessions.remove(ses.getId());
logger.debug("Unregisterd seesion:"+ses.getId());
}
public HttpSession findSession(String nick){
Iterator it = sessions.values().iterator();
while(it.hasNext()){
HttpSession session = (HttpSession) it.next();
if(session.getAttribute("registration")!=null){
Registration registration = (Registration) session.getAttribute("registration");
if(registration.getNick().equals(nick))
return session;
}
}
return null;
}
public HttpSession findSession(int id_registration){
Iterator it = sessions.values().iterator();
while(it.hasNext()){
HttpSession session = (HttpSession) it.next();
if(session.getAttribute("registration")!=null){
Registration registration = (Registration) session.getAttribute("registration");
if(registration.getId()==id_registration)
return session;
}
}
return null;
}
public int countOnlineUsers(){
Iterator it = sessions.values().iterator();
int count=0;
while(it.hasNext()){
HttpSession session = (HttpSession) it.next();
if(session.getAttribute("registration")!=null)
count++;
}
return count;
}
}
The Tomcat output:
----------------------------
1 Jan 2006 09:27:44. DEBUG [http-9090-Processor24] - Registered new session: 3379890EF0750544FEDBA0201A4E7AA1
1 Jan 2006 09:27:50. DEBUG [http-9090-Processor23] - Registered new session: 79B7B95D97611A6D90756E741394324D
1 Jan 2006 09:27:54. DEBUG [http-9090-Processor24] - Registered new session: 28BBE0D1CBF1817C3F919A9E885A4A0B
etc...
For the session management i have a session listener and a session service.
The listener is configured inside web.xml as attached.
Attached also SessionService, SessionListener and Tomcat out
I would like any insights for solving this problem, or any solutions for replacing this code with something else that would work.
Thanx
C.
<listener>
<listener-class>cy.dating.utils.SessionListener</listener-class>
</listener>
SessionListener:
------------------------
import javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;
public class SessionListener implements HttpSessionListener{
public void sessionCreated(HttpSessionEvent event) {
SessionService.getInstance().addSession(event.getSession());
}
public void sessionDestroyed(HttpSessionEvent event) {
SessionService.getInstance().removeSession(event.getSession());
}
}
SessionService:
-----------------------
import ....domainobjects.Registration;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.log4j.Category;
public class SessionService {
private static SessionService ourInstance = new SessionService();
private HashMap sessions = new HashMap();
private static Category logger = Category.getInstance(SessionService.class);
public static SessionService getInstance() {
return ourInstance;
}
private SessionService() {
}
public void addSession(HttpSession ses){
sessions.put(ses.getId(), ses);
logger.debug("Registered new session: "+ses.getId());
}
public void removeSession(HttpSession ses){
sessions.remove(ses.getId());
logger.debug("Unregisterd seesion:"+ses.getId());
}
public HttpSession findSession(String nick){
Iterator it = sessions.values().iterator();
while(it.hasNext()){
HttpSession session = (HttpSession) it.next();
if(session.getAttribute("registration")!=null){
Registration registration = (Registration) session.getAttribute("registration");
if(registration.getNick().equals(nick))
return session;
}
}
return null;
}
public HttpSession findSession(int id_registration){
Iterator it = sessions.values().iterator();
while(it.hasNext()){
HttpSession session = (HttpSession) it.next();
if(session.getAttribute("registration")!=null){
Registration registration = (Registration) session.getAttribute("registration");
if(registration.getId()==id_registration)
return session;
}
}
return null;
}
public int countOnlineUsers(){
Iterator it = sessions.values().iterator();
int count=0;
while(it.hasNext()){
HttpSession session = (HttpSession) it.next();
if(session.getAttribute("registration")!=null)
count++;
}
return count;
}
}
The Tomcat output:
----------------------------
1 Jan 2006 09:27:44. DEBUG [http-9090-Processor24] - Registered new session: 3379890EF0750544FEDBA0201A4E7AA1
1 Jan 2006 09:27:50. DEBUG [http-9090-Processor23] - Registered new session: 79B7B95D97611A6D90756E741394324D
1 Jan 2006 09:27:54. DEBUG [http-9090-Processor24] - Registered new session: 28BBE0D1CBF1817C3F919A9E885A4A0B
etc...