jsp中如何使用户未登录时,直接打开其他页面自动跳转到登陆页面。
jsp中如何使用户未登录时,直接打开其他页面自动跳转到登陆页面。登陆之后就可以正常访问相关页面~请高手指点...
jsp中如何使用户未登录时,直接打开其他页面自动跳转到登陆页面。登陆之后就可以正常访问相关页面~请高手指点
展开
6个回答
展开全部
图显IP网:
最简单直接而且也是最好的的做法就是使用filter来实现,给个例子:
package com.jackshow.net.manage.web.filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import com.jackshow.net.manage.model.sysuser.SysUser;
import com.jackshow.net.util.Constants;
public class LoginFilter implements Filter {
private static final Logger log = Logger.getLogger(LoginFilter.class);
String co_loginPage=null;
protected FilterConfig filterConfig=null;
public void init(FilterConfig config) throws ServletException {
this.filterConfig=config;
this.co_loginPage = filterConfig.getInitParameter("co_loginPage");
}
public boolean isFilterPath(HttpServletRequest req){
String reqUri = req.getRequestURI();
if(reqUri != null && reqUri.indexOf(Constants.GLOBAL_NOT_FILTER_PATH) != -1){
return true;
}else{
return false;
}
}
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
HttpServletRequest hreq=(HttpServletRequest)req;
HttpServletResponse hres=(HttpServletResponse)res;
HttpSession session=hreq.getSession();
String userId="";
if(!isFilterPath(hreq)){
try{
SysUser sysUser=(SysUser)session.getAttribute(Constants.WEB_LOGIN_USER_INFO);
if(sysUser!=null){
userId=sysUser.getUserId();
if(!userId.equals("")){
chain.doFilter(req, res);
}else{
String url=hreq.getContextPath()+co_loginPage;
hres.sendRedirect(url);
}
}else{
String url=hreq.getContextPath()+co_loginPage;
hres.sendRedirect(url);
}
}catch(Exception e){
e.printStackTrace();
log.error(e);
}
}else{
chain.doFilter(req, res);
}
}
public void setFilterConfig(final FilterConfig filterConfig){
this.filterConfig=filterConfig;
}
public void destroy() {
this.filterConfig=null;
}
}
最简单直接而且也是最好的的做法就是使用filter来实现,给个例子:
package com.jackshow.net.manage.web.filters;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import com.jackshow.net.manage.model.sysuser.SysUser;
import com.jackshow.net.util.Constants;
public class LoginFilter implements Filter {
private static final Logger log = Logger.getLogger(LoginFilter.class);
String co_loginPage=null;
protected FilterConfig filterConfig=null;
public void init(FilterConfig config) throws ServletException {
this.filterConfig=config;
this.co_loginPage = filterConfig.getInitParameter("co_loginPage");
}
public boolean isFilterPath(HttpServletRequest req){
String reqUri = req.getRequestURI();
if(reqUri != null && reqUri.indexOf(Constants.GLOBAL_NOT_FILTER_PATH) != -1){
return true;
}else{
return false;
}
}
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
HttpServletRequest hreq=(HttpServletRequest)req;
HttpServletResponse hres=(HttpServletResponse)res;
HttpSession session=hreq.getSession();
String userId="";
if(!isFilterPath(hreq)){
try{
SysUser sysUser=(SysUser)session.getAttribute(Constants.WEB_LOGIN_USER_INFO);
if(sysUser!=null){
userId=sysUser.getUserId();
if(!userId.equals("")){
chain.doFilter(req, res);
}else{
String url=hreq.getContextPath()+co_loginPage;
hres.sendRedirect(url);
}
}else{
String url=hreq.getContextPath()+co_loginPage;
hres.sendRedirect(url);
}
}catch(Exception e){
e.printStackTrace();
log.error(e);
}
}else{
chain.doFilter(req, res);
}
}
public void setFilterConfig(final FilterConfig filterConfig){
this.filterConfig=filterConfig;
}
public void destroy() {
this.filterConfig=null;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询