filter过滤器怎么写

时间:2021-04-21 20:44:02

package com.wh.filter;

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.wh.pojo.User;

/**
* Servlet Filter implementation class loginfilter
*/

public class loginfilter implements Filter {
int i=0;
/**
* Default constructor.
*/
public loginfilter() {
// TODO Auto-generated constructor stub
}

/**
* @see Filter#destroy()
*/
public void destroy() {
// TODO Auto-generated method stub
}/*** @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)*/

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
i++;
System.out.println("filter......"+i);
HttpServletRequest request2 = (HttpServletRequest) request;
HttpServletResponse response2 = (HttpServletResponse) response;
String url = request2.getRequestURI();//获取request请求的的url
int index = url.indexOf(request2.getContextPath()+"/login.jsp");//如果url里面有indexof括号里面的地址,返回值为0,如果没有返回值为-1;
if(index!=-1)
{
chain.doFilter(request, response);//过滤器放行
return;//放行就return结束,不然下面还有放行的,上面的就白判断了

}
HttpSession session = request2.getSession();
User user=(User) session.getAttribute("user");//注意要在servlet里面写了setAttribute()函数,不然getAttribute()不到值HttpSession session=request.getSession(); session.setAttribute("user", us2);
                                                                 
System.out.println(user);
if(user==null){
response2.sendRedirect(request2.getContextPath()+"/login.jsp");
return;
}


chain.doFilter(request, response);
}

/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
// TODO Auto-generated method stub
}

}

//下面是一个登录的servlet

package com.wh.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wh.Dao.UserDao;
import com.wh.Daoimpl.UserDaoImpl;
import com.wh.pojo.User;

public class UserServlet extends HttpServlet {

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* Constructor of the object.
*/
public UserServlet() {
super();
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name=request.getParameter("username");
String password=request.getParameter("password");
//System.out.println(name);
//System.out.println(password);
User user = new User();
User us2 = new User();
user.setName(name);
user.setPassword(password);
UserDao uDao =new UserDaoImpl();
us2=uDao.login(user);
//System.out.println(us2);
if(us2.getName()!=null){
//
HttpSession session=request.getSession();
session.setAttribute("user", us2);
response.sendRedirect(request.getContextPath()+"/jsp/index.jsp");
//request.getRequestDispatcher("index.jsp").forward(request, response);
}
else{
response.sendRedirect(request.getContextPath()+"/jsp/error.jsp");

}


}
}