站内搜索: 请输入搜索关键词

当前页面: 开发资料首页J2EE 专题JSF中,处理用户登陆了,普通做法就是在SESSION做个标记,然后每个页都判断,我感觉这个太麻烦了,

JSF中,处理用户登陆了,普通做法就是在SESSION做个标记,然后每个页都判断,我感觉这个太麻烦了,

摘要: JSF中,处理用户登陆了,普通做法就是在SESSION做个标记,然后每个页都判断,我感觉这个太麻烦了,


JSF中,处理用户登陆了,普通做法就是在SESSION做个标记,然后每个页都判断,我感觉这个太麻烦了,JSF中有处理这方面的其他方法吗,
难道还要在每个页中都判断SESSION吗


做个过滤器.


交给服务器,把jsp文件放在一个文件夹内,使用服务器的 file security realm,只有登陆的用户才能访问jsp文件


来个过滤器吧,过滤器的功能就是对每次请求操作都会自动判断session是否不为空,如果为空就跳到首页。
* 用于判断session是否过期的过滤器
*
*
*/
/*
* 如何调用:需在项目的web.xml中加入以下内容
*
accessControl
publicClass.AccessControlFilter

loginPage
/index.jsp




accessControl
*.jsp



accessControl
*.do

*/
public class AccessControlFilter extends HttpServlet implements Filter {
protected FilterConfig filterConfig;
private String loginPage;
private static final String CONTENT_TYPE = "text/html; charset=GBK";
public void init(FilterConfig config) throws ServletException {
this.filterConfig = config;
loginPage = config.getInitParameter("loginPage");
if (loginPage == null) {
throw new ServletException("loginPage init param missing");
}
}
public void doFilter(final ServletRequest req, final ServletResponse
res, FilterChain chain) throws IOException,
ServletException {


HttpServletRequest hreq = (HttpServletRequest) req;
HttpServletResponse hres = (HttpServletResponse) res;
hres.setContentType(CONTENT_TYPE);
hreq.setCharacterEncoding("GBK");

PrintWriter out = hres.getWriter();
HttpSession session = hreq.getSession();
String uri = hreq.getRequestURI();
User user = (User) session.getAttribute("user");
if(user!=null || uri.equals("/IndexAction.do") || uri.equals("/exitSystem.do")){//检查是否登录
chain.doFilter(req,res);
return;
}
else if(uri.equals("/index.jsp") || "/".equals(uri) || "".equals(uri) || uri.equals("//index.jsp")){
chain.doFilter(req,res);
return;
}
else{
out.println("<script language='javascript'>alert('您还没有登录或很久未操作系统,请重新登录!');parent.parent.location.href='/index.jsp';</script>");

}
}
public void destroy() {
this.filterConfig = null;
}

public void setFilterConfig(final FilterConfig filterConfig) {
this.filterConfig = filterConfig;
}

}
这是我们原来项目中用到的,我改了下,给你参考下


用filter好



我用不了


↑返回目录
前一篇: 在JSP页面使用Bean问题
后一篇: Hibernate问题