如何用Filter實現對註冊用戶是否登錄的過濾 – JAVA編程語言程序開發技術文章

package filter;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.* ;
public class UserFilter implements Filter
{
 
 public void init(FilterConfig filterConfig) throws ServletException
 {
 
 }
 
 public void doFilter(ServletRequest request,
            ServletResponse response,
            FilterChain chain) throws ServletException,IOException
 {
  response.setContentType("text/html ;charset=UTF-8");
  request.setCharacterEncoding("UTF-8");
  PrintWriter out=response.getWriter();
 
  HttpServletRequest req=(HttpServletRequest)request;
  HttpSession session=req.getSession();
  //System.out.println(session.getAttribute("user"));
 
 
  if(session.getAttribute("user")!=null){
   RequestDispatcher rd = null;
   rd = request.getRequestDispatcher("/index.jsp"); //定向的頁面
   rd.forward(request, response);
  }
  else{
   RequestDispatcher rd = null;
   rd = request.getRequestDispatcher("/login.jsp"); //定向的頁面
   rd.forward(request, response);
  
  }
 }
 public void destroy(){}
}

 

作者“TxjDsk”
 

發佈留言