• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

get a bean stored in ServletContext with JSF

 
Greenhorn
Posts: 15
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am working on a very simple website, it will contain just a few slideshows. The information about the images is stored in an xml file. I have created a servlet (load-on-startup) who reads the file, generates the beans and stores the Lists in the ServletContext. The beans should be avalilable since all the pages, and their information will not change dynamically.

I don't know how can I get these Lists from the ServletContext with JSF. I am trying ot create a managed-bean in the faces-config.xml, but I think I am doing something completely wrong. I would like to get a managed-bean with the lists from the faces-config and use a tag datatable to show the images.

Could you help me please?

Thank you in advance

PS: My Bean

public class Gallery {

private List<ItemGallery> inventary;

public Gallery(String xmlFile){
XMLSAXParser xml = new XMLSAXParser(xmlFile);
inventary = xml.getGalery();
}

public List<ItemGallery> getItemsSlideshow(int beginElem){

List<ItemGallery> res = new ArrayList<ItemGallery>();

res.addAll(inventary.subList(beginElem, inventary.size()-1));
res.add(inventary.get(inventary.size()-1));
res.addAll(inventary.subList(0, beginElem));

return res;

}

}


My Servlet:

public class InitServlet extends HttpServlet{
...

public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException {

ServletContext sc = getServletConfig().getServletContext();
String confFenetres = getServletConfig().getInitParameter("confFenetres");
//the xml is stored in webapps
File f = new File(sc.getRealPath("/"));
String configFilePath = f.getParent();

String path = configFilePath+File.separator+confFenetres;

//Contains the Lists, Uses SAXParser to read the file and generate the Objects
Galery gFenetres=new Galery(path);

sc.setAttribute("fenetres",gFenetres);


}
}
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Julio!

I'm not sure I understand what you mean by "bean stored in ServletContext". I do this kind of stuff using application-scope objects, but it looks like what you're actually attempting is to get the servlet init parameters, and I think you're basically doing that the right way.

Some other stuff I noticed that you may want to do in a cleaner way, however. If you want to read/display objects from inside a WAR, it's better to use the HttpServletRequest getResource/getResourceAsStream methods instead of using filesystems methods. For one thing, a "pure" WAR isn't a directory, it's a ZIP file, so the application code would break. Plus it's less work to get an inputStream already opened and ready for use than to do it all yourself.

One thing you should NEVER do is WRITE into a WAR. Even if it isn't zipped, that's trouble.
 
Julio Ayala
Greenhorn
Posts: 15
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all, thank you for your reply. I really love this forum.

I wasn't clear enough. Please, let me explain the provisional solution that I have found, and why I think it is not a good one.

The init parameter that I send is the path of the xml file with all the information about the images(id, path, thumbnails path and some text...) and it is stored outside the war. I think this works fine, my problem is that I don't know how to recover in the JSP the beans that I generated on the startup. To solve this I have modified a little bit the servlet:


The bean Auxiliar has getters and setters that use static attributes:


After that I declare this bean in the faces-config.xml like this:


And now I am able to get the information since the JSP like


This configuration lets me get the beans that I created on my servlet at the startup, and it works well, but I think there should be another way to do this properly (not using an Object to get static attributes). I want to save somewhere the beans that I create at the startup and use these beans in the datatable, but I am absolutely begginer in JSF and I don't know how to do it.

I don't want to read the xml file several times, because its content will not change dynamically. At the beggining I tried to save the beans in the servlet context, but I couldn't get the beans since <h:dataTable value="#{???}"

Please, let me know if something is not clear or if some details are missing.

Thank you in advance
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK. I think you can do most of this without JSF and then let JSF use it. Like so:

Create a ServletContextListener. In its context initialization method, retrieve the initialization parameter that points to the XML file, load the XML using apache Digester, JAXB, XMLBeans, or however you like to parse XML. save that in the application scope (ServletContext) attributes under whatever name you want - "aux" for example.

An application-scope bean created in non-JSF code is usable by JSF and is effectively identical to a JSF managed application scope bean. You can reference it directly in the View definition, as well as run through the FacesContext to retrieve it in the backing bean. About the only thing you can't do is inject it, since it's not a managed bean.
 
Julio Ayala
Greenhorn
Posts: 15
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much!!I didn't know that a bean created in non-JSF code is usable by JSF . With your comment and this other post:

https://coderanch.com/t/210505/JSF/java/Managed-bean-vs-Reference-bean

I have deleted the horrible class "Auxiliar" and used a referenced-bean instead. The final code looks like this:

the servlet



the faces-config

...
...

And the jsp:
...
<h:dataTable var="item"
value="#{galFenetres.inventary}"
rows="5">

<h:column> ...


Thank you very much again!! you help me a lot Now I can get some rest
 
Tim Holloway
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem! It's why we're here!

Just some final notes.

1. DON'T store config or data files in the Tomcat webapps directory! That's not what it's for, and some future version of Tomcat may malfunction or get annoyed if you do. Plus, if you ever end up porting your webapp to some other server, such as WebLogic, the app will break. The best thing to do is pick a location completely outside of the Tomcat directories and set the web.xml servlet config parameter to be the absolute filesystem location of the config file. For a Unix/Linux system, you might pick someplace like /etc/myapp/config.xml. For Windows, maybe something like C:/myapp/config.xml. Note that backslashes are dangerous in Java, so Windows filename paths are best coded Unix-style with real slashes in them.

2. The dummy servlet approach works, but the recommended way these days is to define a ContextListener class instead, It's simpler to configure and code and you don't have to worry about getting it to autostart.



 
Julio Ayala
Greenhorn
Posts: 15
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK!!Now I use the servletContextListener to create the lists, it takes a the path from a context-param. I also have moved the files to another folder outside Tomcat and the project, and I use a servlet to read and write the images.

Thank you for your advises, everything is much more clean and tidy now.
 
These are not the droids you are looking for. Perhaps I can interest you in a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic