java的session用法
发布网友
发布时间:2022-04-22 10:33
我来回答
共3个回答
热心网友
时间:2022-04-27 14:27
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 com.fz.entity.User;
public class ValidatorFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)arg0;
HttpServletResponse response = (HttpServletResponse)arg1;
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
//判断用户是否登录,如果没有登录,则直接重定向至登录页面,
//如果登录,则将请求交给下一个过滤器或者目标资源
if(user == null||user.getUserName()==null){
response.sendRedirect(request.getContextPath()+"/login.jsp");
}else{
arg2.doFilter(request, response);
}
}
public void init(FilterConfig filterConfig) throws ServletException {
}
以上是filter的代码
以下是在web.xml中的配置
<filter>
<filter-name>validatorFilter</filter-name>
<filter-class>com.fz.filter.ValidatorFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>validatorFilter</filter-name>
<url-pattern>/user/*</url-pattern>
</filter-mapping>
热心网友
时间:2022-04-27 15:45
1.配置web.xml设置请求信息
2.在过滤器写判断语句
热心网友
时间:2022-04-27 17:19
这些在百度里一大堆呢。你不心疼你的分数吗?这个问题很简单啊。