JSF 1.2 get a request scope managed bean in a regular servlet - jsf

How can I get a managed bean in a pleain servlet with JSF 1.2?
I tried several solutions like those proposed here JSF Managed Beans in a Servlet but without success.
I send an XMLHttpRequest from a facelet where the managed bean is well instanciated, but when I tried this:
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Bean bean = (Bean) req.getAttribute("bean");//null
Bean bean = (Bean) getServletContext().getAttribute("bean");//null
}
Is there a way on JSF 1.2?
Thanks a lot

Related

Where to close opened clients or use java garbage collection in JSF?

I use JSF portlets with Liferay. In the bean's constructer, I created some objects and also some clients to access some servers. I don't know where should I deconstruct those objects or use garbage collector and also close those clients when I refreshed the page or redirected any other page.
Thanks for helps.
Don't use the constructor. For sure not if you're using CDI. Also for sure don't rely on GC when it comes to cleaning up expensive resources. Just use #PostConstruct and #PreDestroy annotations on the desired methods. The bean management framework will all by itself explicitly call them when the bean scope starts and ends.
public class Bean {
#PostConstruct
public void init() {
// ...
}
#PreDestroy
public void destroy() {
// ...
}
}
This works on both JSF and CDI managed beans. Only when using #ViewScoped in JSF 2.0-2.1, the #PreDestroy isn't guaranteed to be invoked in all circumstances. In case you're using CDI on a Servlet (i.e. non-Portlet) environment, the OmniFaces #ViewScoped solves this problem of JSF 2.0-2.1 #ViewScoped #PreDestroy fail.

JSF managed upload bean?

I have an upload routine which is working for servlets. Now I was trying to put this routine in a jsf managed bean like this:
public void uploadFile() throws IOException, ServletException{
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
HttpServletRequest request = (HttpServletRequest) externalContext.getRequest();
HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
Part filePart = request.getPart("item");
String filename = getFilename(filePart);
InputStream filecontent = filePart.getInputStream();
//persist the data here
}
when trying to run it on the server, of course there is the error message: "PWC4016: Request.getPart is called without multipart configuration. Either add a #MultipartConfig to the servlet, or a multipart-config element to web.xml"
But I don't know where to put this annotation, neither do I have the name of the jsf generated servlet of my managed bean so I can't put it into the web.xml neither.
Is it generally a bad idea to put this routine into a managed bean or should I stick to the servlet variant?
The #MultipartConfig basically needs to be put on the FacesServlet. You can't do it yourself, but this is already done for the upcoming JSF 2.2, complete with a new standard <h:inputFile> component with ajax support, see also JSF spec issue 802.
Until then, your best bet is grabbing a 3rd party component library or homebrewing a custom component.
Tomahawk <t:inputFileUpload> - see also JSF 2.0 File upload
PrimeFaces <p:fileUpload> - see also How to use PrimeFaces p:fileUpload?
Custom <x:inputFile> - see also Uploading files with JSF 2.0 and Servlet 3.0

What is the relation between session scoped beans and HttpServletRequest?

While trying to figure out how to implement a login filter for a JSF app I saw these 2 lines of code that I didn't understand that much :
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
HttpServletRequest req = (HttpServletRequest) request;
LoginBean login = (LoginBean) req.getSession().getAttribute("login");
}
Assuming that LoginBean class is a session scoped bean named "login" , as I noticed the bean is an attribute for the request , what is the relation between them ? are all session scoped bean saved as "attributes" in request sessions ?
are all session scoped bean saved as "attributes" in request sessions ?
That's correct. JSF is just a MVC framework which is built on top of the bare Servlet API, not an entirely standalone framework which can run without the Servlet API. Even more, the JSF core controller FacesServlet is a fullworthy Servlet, so it definitely requires a servlet container to run. The concept "session" is in the Servlet API provided by HttpSession, so it would make fully sense to store JSF session scoped beans in there instead of reinventing it.
Note that JSF request scoped beans are stored as HttpServletRequest attributes and that JSF application scoped beans are stored as ServletContext attributes.
See also:
Get JSF managed bean by name in any Servlet related class
How do servlets work? Instantiation, sessions, shared variables and multithreading

Direct URL to a JSF bean action

Is there any way to get direct URL to a JSF bean action method?
Basically I want to be able to do the following:
click
And this should invoke a method in a JSF bean. Is it possible?
I'm using JSF 1.2.
Do the job in the constructor of the request scoped bean associated with the view behind the URL.
public class Bean {
public Bean() {
// Here.
}
// ...
}
As long as you've a #{bean.something} in the view, then the bean's constructor will be invoked.

JSF2.0 PostConstructApplicationEvent managed bean is null

We have JSF2.0 in Tomcat6.0 , need to initialize a ApplicationScope Bean while web server is started.
I tried using the PostConstructApplicationEvent processEvent method to initialize the Bean , but the managed bean from faces-config.xml is returning null.
Is there any other better way to instantiate the bean after startup?
Remove any faces-config.xml declarations related to the bean (they will otherwise override the JSF 2.0 annotations) and then annotate the bean with #ManagedBean(eager=true) as follows:
#ManagedBean(eager=true)
#ApplicationScoped
public class Bean {
// ...
}
This way the bean will always be instantiated on JSF webapp startup, without the need to view any page. You can then do the initialization job in the constructor and/or #PostConstruct of the bean.

Resources