JSF Important Interview Questions
By Ramakrishna on Jul 24, 2008 in JSF Interview Questions
Q) How to get current page URL from backing bean?
A) You can get a reference to the HTTP request object via FacesContext like this:
HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
and then use the normal request methods to obtain path information. Alternatively,
context.getViewRoot().getViewId();
will return you the name of the current JSP (JSF view IDs are basically just JSP path names)
Q) How to access web.xml init parameters from jsp page?
A) You can get it using initParam pre-defined JSF EL valiable.
For example, if you have:
<context-param> <param-name>productId</param-name> <param-value>2004Q4</param-value></context-param>
You can access this parameter with #{initParam['productId']} . For example:
Product Id: <h:outputText value=”#{initParam['productId']}”/>
Q) How to access web.xml init parameters from java code?
A) You can get it using externalContext getInitParameter method. For example, if you have:
<context-param> <param-name>connectionString</param-name> <param-value>jdbc:oracle:thin:scott/tiger@cartman:1521:O901DB</param-value></context-param>
You can access this connection string with:
FacesContext fc = FacesContext.getCurrentInstance();
String connection = fc.getExternalContext().getInitParameter(“connectionString”);
Q) How to terminate the session?
A) In order to terminate the session you can use session invalidate method.
This is an example how to terminate the session from the action method of a backing bean:
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
session.invalidate();
return “login_page”;
}
The following code snippet allows to terminate the session from the jsp page:
<% session.invalidate(); %> <c:redirect url=”loginPage.jsf” />
Q) How to implement “Please, Wait…” page?
A) The client-side solution might be very simple. You can wrap the jsp page (or part of it you want to hide) into the DIV, then you can add one more DIV that appears when user clicks the submit button. This DIV can contain the animated gif you speak about or any other content.
Scenario: when user clicks the button, the JavaScript function is called. This function hides the page and shows the “Wait” DIV. You can customize the look-n-fill with CSS if you like.
This is a working example:
<%@ taglib uri=”http://java.sun.com/jsf/core” prefix=”f” %>
<f:loadBundle basename=”demo.bundle.Messages” var=”Message”/>
<html>
<head>
<title>Input Name Page</title>
<script>
function gowait() {
document.getElementById(“main”).style.visibility=”hidden”;
document.getElementById(“wait”).style.visibility=”visible”;
}
</script>
</head>
<body bgcolor=”white”>
<f:view>
<div id=”main”>
<h1><h:outputText value=”#{Message.inputname_header}”/></h1>
<h:messages style=”color: red”/>
<h:form id=”helloForm”>
<h:outputText value=”#{Message.prompt}”/>
<h:inputText id=”userName” value=”#{GetNameBean.userName}” required=”true”>
<f:validateLength minimum=”2″ maximum=”20″/>
</h:inputText>
<h:commandButton onclick=”gowait()” id=”submit”
action=”#{GetNameBean.action}” value=”Say Hello” />
</h:form>
</div>
<div id=”wait” style=”visibility:hidden; position: absolute; top: 0; left: 0″>
<table width=”100%” height =”300px”>
<tr>
<td align=”center” valign=”middle”>
<h2>Please, wait…</h2>
</td>
</tr>
</table>
</div>
</f:view>
</body>
</html>
If you want to have an animated gif of the “Wait” Page, the gif should be reloaded after the form is just submitted. So, assign the id for your image and then add reload code that will be called after some short delay. For the example above, it might be:
<script>
function gowait() {
document.getElementById(“main”).style.visibility=”hidden”;
document.getElementById(“wait”).style.visibility=”visible”;
window.setTimeout(‘showProgress()’, 500);
}
function showProgress(){
var wg = document.getElementById(“waitgif”);
wg.src=wg.src;
}
</script>
….
….
….
<img id=”waitgif” src=”animated.gif”>
Q) How to reload the page after ValueChangeListener is invoked?
A) At the end of the ValueChangeListener, call FacesContext.getCurrentInstance().renderResponse()
Q) How to download PDF file with JSF?
A) This is an code example how it can be done with action listener of the backing bean.
Add the following method to the backing bean:
String filename = “filename.pdf”;// use your own method that reads file to the byte array
byte[] pdf = getTheContentOfTheFile(filename);
FacesContext faces = FacesContext.getCurrentInstance();
HttpServletResponse response = (HttpServletResponse) faces.getExternalContext().getResponse();
response.setContentType(“application/pdf”);
response.setContentLength(pdf.length);
response.setHeader( “Content-disposition”, “inline;filename=\”"+fileName+”\”");
try {
ServletOutputStream out;
out = response.getOutputStream();
out.write(pdf);
} catch (IOException e) {
e.printStackTrace();
}
faces.responseComplete();
}
This is a jsp file snippet:
<h:commandButton immediate=”true” actionListener=”#{backingBean.viewPdf}” value=”Read PDF” />

1 Comment(s)
By sandrar on Sep 10, 2009 | Reply
Hi! I was surfing and found your blog post… nice! I love your blog.
Cheers! Sandra. R.
Thanks