When Managed Bean creates and property of the beans creates? - java

In our application we use jsf,we have
to redirect the user to home page
after their session will be expired.For that i need a path of the home page which i kept in my logout managed bean as a managed bean property.But after session expired if i try to access that it will arise null pointer exception(managed bean becomes null).Then
i have decide to try alternative (i.e)create logout class manually and try to access the property, at that time the property which i wants to access is become null.How can i access
that property?
Please help me.
Thanks in advance.

In addition to the previous answer:
You could use (in web.xml)
<error-page>
<exception-type>javax.faces.application.ViewExpiredException</exception-type>
<location>viewexpired.jsp</location>
</error-page>
Or Context Parameters instead of Session Attributes. See:
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html
http://safebox.guisho.com/jsf-how-to-get-webxml-parameters
Or use (in faces-context.xml)
<managed-bean-scope>application</managed-bean-scope> 
for your bean, so it will stay independent from the session.

Correct way of doing this is declaring exception handler factory in faces-config.xml, then implementing the factory by subclassing javax.faces.context.ExceptionHandlerFactory, and then overriding handle() method in your implementation of javax.faces.context.ExceptionHandlerWrapper.
There you should analyze the exception for the ViewExpiredException class and redirect to your view expired page in that case.

Related

Servlet/JSF: Pass parameter from filter to bean method invoked later

In my JSF application I have implemented a Filter class that redirects non-logged in requests to the login page.
Before I do the redirect, I store some parameters as attributes in the session object. I do this, so I can access them in RequestScoped beans when their methods are called on the login page.
Currently, I do it like this:
httpSession.setAttribute("parameterKey", "text");
And retrieve them in RequestScoped bean by doing:
String parameter = (String)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove("parameterKey");
It works, but I'm not happy with it. Is there a way to accomplish this without filling the session with some junk?

Session expired in spring MVC

How can I handle the session expired in spring MVC and what is the best place to handle it? where can I specify the session time out? It would be great if you could show me by some example.
For sessions timeout I am using 'plain' Servlet API.
Custom javax.servlet.http.HttpSessionListener defined in web.xml and in method sessionCreated on session set timeout using setMaxInactiveInterval (in [s]).
I know it is 'old school', but is simple and working for me.
If you want to get timeout value from spring, there is access to ServletContext from session.
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(session.getServletContext());
applicationContext.getBean("...");
One option is to use the event mechanism in ApplicationContext, you would then register a HttpSessionListener in web.xml that when a session timeouts fires an event in the root WebApplicationContext to all beans that observes that event. Get the root WebApplicationContext using WebApplicationContextUtils.

How to use the "Guessed User name" in tomcat manager in a wicket application?

In Tomcat Manager on the Page "Sessions Administration", there is a column Guessed User name. How can i set this column from within my Wicket Session?
Note: I have successfully overridden AuthenticatedWebSession.toString() and this value is shown on the sessin details page.
I have tried to set AuthenticatedWebSession.userName as recommended here.
Please mind that Wicket session is a different beast than actual servlet session. Creating a "userName" property in wicket session will not work.
You need to access the raw HttpServletRequest from wicket to set the value properly: this is how you do it.
Add this code to your WebPages:
HttpServletRequest request = getWebRequestCycle().getWebRequest().getHttpServletRequest();
request.getSession().setAttribute("userName", userName);
You can create any Java bean and add it to session like this,
session.setAttribute("user", user);
Tomcat will find the bean and whatever prints in toString will show up.
see image here

How to access application scope in http session listeners?

How to get the application scope in the http session listeners in servlets?
Basically I need to remove some values from application scope on automatic session expiry.
Thanks
The Servlet term for “your entire web app at runtime” is context.
In the listener, get servlet context from the session object.
Invoking session.getServletContext().removeAttribute() should solve your problem.
You can get the context this way:
void sessionDestroyed(HttpSessionEvent se){
ServletContext context=HttpServlet.getServletContext();
// Add/delete objects from context
context.removeAttribute("xyz")
}
se.getSession().getServletContext();

Spring "session" scope of a bean

It seems to me that "session" scope is another means to keep objects in session as
using setAttrubute / getAttribute
Correct?
You know, dont know why, it does not work for me.
<bean id="sabreUser" class="util.MyUser" factory-method="getSomeUser" scope="session">
<const args...>
What I see is that after the initialization and initial deploy the MyUser properties are correct.
Then, in the first session I change MyUser property, the session is closed.
The second session runs and it sees the last set value from the previous session!
What does that mean?
I expect this object to be initialized whenever a new session starts. But it better looks as singleton, though you see - "session" attribute is set.
I can see the cause of the problem in that a Servlet's fields is initialized with #Autowired
so, once it is initialized, every other session will see its fields set and does not "ReWire" this properties. It happens once? How to overcome this problem?
The Spring session does not exactly match the HttpSession, and even the Spring documentation on the #SessionAttributes annotation says that it might be stored in the session or "some conversational storage". I got that from The Spring docs for 2.5
I've basically quit trying to make sense of it, and just got on with my life, if I want something stored in the HttpSession, I just have Spring inject the HttpSession to me, assuming you're using Spring MVC its pretty easy, instructions on the same page.
Session-scoped beans are beans that live throughout the http session. They are stored in the session via setAttribute in some way.
So - yes.
Session scoped beans are stored in Http Session by Spring framework. This scope is valid only in the context of Web application.It also works for Portlet envionments . When using in Portlet environment, there are two notions of session, application scope and portlet scope (default).

Categories

Resources