Thursday July 17, 2008
Retrieving JSF Session variables in your servlets
Quite often in your JSF Application you need to call a backend servlet to perform a certain task. Perhaps in the back-end servlet you may want would also need to retrieve a variable out of the JSF's SessionBean1 properties.
Here's a quick capture showing you how to retrieve a variable out of JSF's SessionBean1 object.
Create the following global variables for your servlet class:
private ServletContext context;
private Lifecycle lifecycle;
private ServletConfig servletConfig;
private FacesContextFactory facesContextFactory;
private FacesContext facesContext;
In your servlets init(...) method add the following snippets of code:
this.servletConfig = config;
this.context = config.getServletContext();
Then capture and retrieve the faces context:
try {
facesContextFactory =
(FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
} catch (FacesException e) {
Throwable rootCause = e.getCause();
if (rootCause == null) {
throw e;
} else {
throw new ServletException(e.getMessage(), rootCause);
}
}
try {
LifecycleFactory lifecycleFactory =
(LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
String lifecycleId =
servletConfig.getServletContext().getInitParameter(FacesServlet.LIFECYCLE_ID_ATTR);
if (lifecycleId == null) {
lifecycleId = LifecycleFactory.DEFAULT_LIFECYCLE;
}
lifecycle = lifecycleFactory.getLifecycle(lifecycleId);
} catch (FacesException e) {
Throwable rootCause = e.getCause();
if (rootCause == null) {
throw e;
} else {
throw new ServletException(e.getMessage(), rootCause);
}
}
Add the following code to your destroy() method:
facesContextFactory = null;
lifecycle = null;
servletConfig = null;
context = null;
facesContext = null;
Lastly, add the following methods to your servlet class to retrieve your chosen variables out of the SessionBean1 object (your JSF Session Variables):
/** Evaluate and return the given value binding expression
* @param context The FacesContext for this web app
* @param expr The value binding expression to be evaluated, such as "Sum is #{foo.sum}"
* @return The value of the expression
*/
private Object getValue(FacesContext context, String expr) {
return getValue(context, expr, expr.getClass());
}
protected Object getValue(FacesContext context, String expr, Class c) {
ValueExpression ve = context.getApplication().getExpressionFactory().createValueExpression(
context.getELContext(), expr, c);
if (ve != null) {
return ve.getValue(context.getELContext());
}
return null;
}
/** Evaluate and return the given value binding expression
* @param context The FacesContext for this web app
* @param expr The value binding expression to be evaluated, such as "Sum is #{foo.sum}"
* @return The value of the expression
*/
protected void setValue(FacesContext context, String expr, String value) {
ValueExpression ve = context.getApplication().getExpressionFactory().createValueExpression(
context.getELContext(), expr, value.getClass());
if (ve != null) {
ve.setValue(context.getELContext(), value);
}
}
/** Find the named JSF managed bean
* @param context The FacesContext for this web app
* @param expr The name of the managed bean to be found, such as "SessionBean1"
* @return The managed bean
*/
protected Object getBean(FacesContext context, String name) {
ELResolver resolver = context.getApplication().getELResolver();
return resolver.getValue(context.getELContext(), null, name);
}
Then in your doGet or doPost method(s) add:
// Acquire the FacesContext instance for this request
facesContext = facesContextFactory.getFacesContext(servletConfig.getServletContext(), request, response, lifecycle);
Now for the fun part;
To retrieve a variable out of the SessionBean class say, name, you can use the following snippet of code:
String name = (String) getValue(facesContext, "#{SessionBean1.name}");
You can also update your JSF session variables as well via the following snippet of code:
setValue(facesContext, "#{SessionBean1.name}", name.toString());
Wella, Done!
Posted at 03:41PM Jul 17, 2008 by Chris Fleischmann in GlassFish | Comments[5]