I am developing a web app using jsf technology. I want a method of my backing bean to be
invoked when the home page of my app is displayed. Usually, a backing bean method
gets involved when a user clicks on a link or a button.
In short I want my backing bean to get some data from the database and send it to the
jsf page, and I want this to occur when a user invokes the home page link
of my app.
Here is how I wanted to solve the problem: use a servlet that is executed when
the expected link is invoked, and call the backing bean from that servlet; but the problem
is I am having some difficulties with the url-pattern of the servlet in the web.xml file. The home page link of my app is: home.jsf. The name of my servlet is
HomeServlet and here is how I configured it in the web.xml file:
<servlet>
<servlet-name>HomeServlet</servlet-name>
<servlet-class>utils.HomeServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HomeServlet</servlet-name>
<url-pattern>/home.jsf</url-pattern>
</servlet-mapping>
So when I run the app, the servlet is executed, but my home.jsf page is empty (completely blank). I don’t know why.
So what I want to know is:
-
Am I using the right approach for this issue?
-
If yes, which url-pattern should I use?
No, this is definitely not the right approach. You’re working your way around JSF. You’re supposed to do the job just in the constructor or
@PostConstructmethod of the request or view scoped JSF managed bean associated with the view.E.g.
Whenever JSF encounters a
#{home.someproperty}reference in thehome.xhtml(orhome.jsp) for the first time, then the bean will just be constructed.When developing with JSF, you shouldn’t have any need to develop other servlets. In JSF, the
FacesServletis the sole servlet which already does all the necessary request/response and model/view controlling job.