SF, günümüzün en popüler web framworklerindendir. Java’nın gücünü ve tasarımın esnekliğini JavaServer Faces ile kullanabilirsiniz. JSF’de oturum yönetimi yani kullanıcı adı ve parola girildikten sonra erişilen korunumlu sayfaları olan uygulamalar geliştirmenin bir çok yöntemi vardır. Örneğin çok basit bir yöntem; her sayfanın başına session’da istenilen bir değerin var olup olmadığına bakılabilir. Aşağıdaki örnekte, bu yöntemden çok daha farklı bir yöntemle yani JSF’ye özgü bir yöntemle oturum yönetiminin nasıl yapılacağı anlatılacaktır.
Neler Gerekli;
- Apache Tomcat (veya başka bir uygulama sunucusu)
- JSF Kütüphaneleri
JSF ortamının nasıl hazırlanacağı ile ilgili probleminiz varsa bu adrese göz atabilirsiniz.
Proje’nin dosya yapısı aşağıdaki şekilde olacaktır. Siz de aynı isimli dosyaları oluşturabilirsiniz.

web.xml
userManagement
index.html
index.htm
index.jsp
default.html
default.htm
default.jsp
Faces Servlet
javax.faces.webapp.FacesServlet
1
Faces Servlet
*.jsf
faces-config.xml
net.bt_tr.PhaseList
/admin/*
logout
/error.jsp
/login.jsp
diger
/admin/index.jsp
/login.jsp
loggedin
/admin/index.jsp
loginfailure
/error.jsp
login
net.bt_tr.Login
session
Burada açıklanması gereken bir iki nokta var. İlki; <lifecycle> arasında PhaseListener java sınıfının uygulamaya dahil edilmesidir. Bir diğer önemli nokta ise <navigation-rule> bölümleridir. <from-view-id>/admin/*</from-view-id> kuralının olduğu navigation’u dikkatle inceleyiniz. /admin/* klasörü içindeki her dosya PhaseListener ile kontrol edilmekte, eğer action olarak logout değeri gelirse, /error.jsp dosyasına yönlendirme yapılmaktadır.
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
Login
error.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
Hata Sayfası
Lütfen giriş yapınız...
detay.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
Insert title here
Burada sadece detay olacak...
admin/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
İçerik Sayfası
Yukarıdaki jsp sayfalarının yönlendirme veya oturum yönetimi ile ilgili herhangi bir komut veya sınıf içermediğine dikkat ediniz. Sadece login.jsp sayfasında kullanıcının giriş yapabileceği bir form yer almakta, diğer sayfaları herhangi bir espirisi olmayan salt jsf sayfalarıdır.
Şimdi sıra geldi Java class’larına,
Login.java
package net.bt_tr;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
public class Login {
String userName;
String password;
public Login() {
this.userName = "";
this.password = "";
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static boolean isLogin() {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
HttpSession httpSession = request.getSession(false);
String uName = (String) httpSession.getAttribute("userName");
System.out.println(uName);
if(uName != null && uName.equals("veli")) {
return true;
}
else
return false;
}
public String loginCheck() {
if(userName.equals("veli")) {
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
HttpSession httpSession = request.getSession(false);
httpSession.setAttribute("userName", "veli");
return "loggedin";
}
else
return "loginfailure";
}
}
PhaseList.java
package net.bt_tr;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
public class PhaseList implements PhaseListener {
private static final long serialVersionUID = 1L;
public void afterPhase(PhaseEvent event) {
FacesContext fc = event.getFacesContext();
if(!loggedIn(event)) {
System.out.println("Yetki yok çıkılacak");
NavigationHandler nh = fc.getApplication().getNavigationHandler();
nh.handleNavigation(fc, null, "logout");
}
}
private boolean loggedIn(PhaseEvent event) {
return Login.isLogin();
}
public void beforePhase(PhaseEvent arg0) {
;
}
public PhaseId getPhaseId() {
return PhaseId.RESTORE_VIEW;
}
}
Uygulamanın kodlarını buradan edinebilirsiniz.