Search your topic in Java2share
Home > FAQs
eXTReMe Tracker

Java Server Faces FAQs

Untitled Document

Q17. How to terminate the session?

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:
public String logout() {
FacesContext fc = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) fc.getExternalContext().getSession(false);
return "login_page";
The following code snippet allows to terminate the session from the jsp page:
<% session.invalidate(); %> <c:redirect url="loginPage.jsf" />

Q18. How to implement "Please, Wait..." page?

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="" prefix="h" %>
<%@ taglib uri="" prefix="f" %>
<f:loadBundle basename="demo.bundle.Messages" var="Message"/>
<title>Input Name Page</title>
function gowait() {
<body bgcolor="white">
<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:commandButton onclick="gowait()" id="submit" action="#{GetNameBean.action}" </h:form>
<div id="wait" style="visibility:hidden; position: absolute; top: 0; left: 0">
<table width="100%" height ="300px">
<td align="center" valign="middle">
<h2>Please, wait...</h2>

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:

function gowait() {
window.setTimeout('showProgress()', 500);
function showProgress(){
var wg = document.getElementById("waitgif");
<img id="waitgif" src="animated.gif">

Q19. How to reload the page after ValueChangeListener is invoked?

At the end of the ValueChangeListener, call FacesContext.getCurrentInstance().renderResponse()

Q20. How to download PDF file with JSF?

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:

public void viewPdf(ActionEvent event) {
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().response.setContentType("application/pdf");
response.setHeader( "Content-disposition", "inline; filename=\""+fileName+"\"");
try {
ServletOutputStream out;
out = response.getOutputStream();
} catch (IOException e) {
This is a jsp file snippet:
<h:commandButton immediate="true" actionListener="#{backingBean.viewPdf}" value="Read