Reputation: 379
My problem is that one : at every http request, a new session scoped bean is created and I don't know why.
Here is my jsf index page :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
<link type="text/css" rel="stylesheet" href="css/default.css"/>
</h:head>
<h:body>
<p:growl autoUpdate="true" showDetail="true" globalOnly="true"/>
<h:form id="f_main">
<ui:include src="#{pageBean.page}.xhtml"/>
</h:form>
</h:body>
</html>
Here is my PageBean
package web.bean.system;
import org.apache.log4j.Logger;
import web.bean.AbstractBean;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class PageBean extends AbstractBean {
private static final long serialVersionUID = -882977117976414497L;
private static final Logger LOG = Logger.getLogger(PageBean.class);
public static final String HOME = "home";
private static int IT = 0;
private String page;
public PageBean() {
LOG.debug(IT++);
this.page = HOME;
}
public String getPage() {
LOG.debug(page);
return this.page;
}
public void setPage(String page) {
LOG.debug(page);
this.page = page;
}
}
In this case, the home page is empty.
But when I take a look at the logs after refreshing a lot of time, I can see that a new bean is created for every http request.
I have verified that I realy use javax.faces.bean and not an other package but I don't know why It doesn't work...
Have you any solution for me ?
Upvotes: 0
Views: 2063
Reputation: 1109695
That may happen if the HTTP session is not properly maintained between the client and the server. Before all, first learn how HTTP sessions work by carefully reading the "HttpSession" section of this answer: How do servlets work? Instantiation, sessions, shared variables and multithreading.
Now, you should understand that they're by default backed by cookies. You should now also understand that if cookies are not maintained by the client or instantly destroyed by the server for some reason, then the session won't be maintained across requests.
If you're using a bit modern webbrowser with builtin web developer toolset, press F12 to show it up and open the "Net"/"Network" tab. Look in the response header for Set-Cookie
and in subsequent request header for Cookie
. If Cookie
is absent in request header and thus the server returns a new Set-Cookie
header on the response, then it means that the client does not support cookies. Or if the right Cookie
header is present and the server still returns a new Set-Cookie
header on every response, then it means that the server's code has somewhere a line which calls HttpSession#invalidate()
on every request (perhaps a homegrown authentication filter which is written by a starter).
Upvotes: 3