判斷使用者是否登入 及 解決使用者重複登入問題
解決使用者是否登入問題:(寫一個過濾器實現)
public class Check_User_LoginFilter implements Filter {
protected FilterConfig filterConfig;
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
this.filterConfig = arg0;
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest sr = (HttpServletRequest) request;
HttpSession session = sr.getSession(); // 獲得session
Register user = (Register) session.getAttribute("userInfo");
SessionListener sl = new SessionListener();
// 判斷使用者是否登入,如果沒有,
if (user == null || user.equals("")) {
// 獲鎝當前的URL
String strURL = sr.getRequestURL().toString();
// 判斷是否為index.jsp或userLogin.do
if (strURL.indexOf("/index.jsp") == -1) {
if (strURL.indexOf("/userLogin.do") == -1) {
ServletContext sc = filterConfig
.getServletContext();
RequestDispatcher rd = sc
.getRequestDispatcher("/work/index.jsp");
try {
rd.forward(request, response); // 轉發到index.jsp讓使用者登入
// chain.doFilter(request, response);
return;
} catch (ServletException sx) {
filterConfig.getServletContext().log(
sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(
iox.getMessage());
}
}
}
}
chain.doFilter(request, response);
}
public void destroy() {
// TODO Auto-generated method stub
this.filterConfig = null;
}
}
<!-- WEBXML 判斷使用者登入 -->
<filter>
<filter-name>Check_User_LoginFilter</filter-name>
<filter-class>
com.wp.app.util.Check_User_LoginFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>Check_User_LoginFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Check_User_LoginFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>Check_User_LoginFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
解決使用者重複登入問題:(配置監聽器實現,此處是如果重複登入將T掉上一個使用者)
public class SessionListener implements HttpSessionListener{
private static java.util.Hashtable hUserName = new Hashtable();
public void sessionCreated(HttpSessionEvent se){
System.out.println( "已建立新使用者session -- " + se);
}
public void sessionDestroyed(HttpSessionEvent se){
hUserName.remove(se.getSession());
System.out.println( "刪除使用者session -- " + se);
}
public synchronized static boolean isLogined(HttpSession session,String sUserName){
boolean flag = false;
if(hUserName.containsKey(sUserName)){
flag = true;
HttpSession vsession = (HttpSession)hUserName.get(sUserName);
try {
vsession.invalidate();
System.out.println("使用者 - " + sUserName + " || session 已存在 - " + session);
System.out.println("已設定 - " + sUserName + " || session 失效並移除" );
}
catch (Exception ex){}
}
else{
flag = false;
}
hUserName.remove(sUserName);
hUserName.put(sUserName,session);
System.out.println("建立使用者 - " + sUserName + " || session - " + session);
return flag;
}
public synchronized static boolean deleteUserName(HttpSession session,String sUserName)
{
System.out.println("刪除使用者 - " + sUserName + " || session - " + session);
hUserName.remove(sUserName); //刪除使用者
return true;
}
public synchronized static boolean isOnline(HttpSession session,String sUserName){
boolean flag = false;
HttpSession vsession = null;
try{
vsession = (HttpSession) hUserName.get(sUserName);
}catch(Exception e){
}
if(vsession != null){
if (session.getId().equals(vsession.getId())) {
flag = true;
} else {
flag = false;
}
}
return flag;
}
}
<!-- WEBXML 配置監聽器 -->
<listener>
<listener-class>com.lihong.wp.web.action.loginAction.SessionListener</listener-class>
</listener>