一,过滤器(Filter): 1.用于在Servlet之外对Request 或 Response 进行修改。主要用于对用户请求进行预处理,也可以对HttpServletResponse进行后处理。 2.过程:对用户请求进行预处理,接着将请求交给Servlet进行处理并生成响应,最后Filter再对服务器响应进行后处理。 3.在一个web应用中可以开发编写多个Filter,这些Filter组合起来称之为一个Filter链。 实例:请求乱码的处理,用户非法访问的拦截 4.在HttpServletRequest 到达 Servlet之前,拦截客户的HttpServletRequest。 根据需要检查HttpServletRequest,也可以修改HttpServletRequest头和数据。 5.在HttpServletResponse 到达客户端之前,拦截 HttpServletResponse。根据需要检查HttpServletResponse,也可以修改HttpServletResponse头和数据。
@WebFilter("/One")
public class FilterTest implements Filter {
@Override
public void init(FilterConfig fc) throws ServletException{
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
System.out.println("正在过滤/One 链接的请求资源");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("Filter处理响应");
}
@Override
public void destroy() {
}
}
@WebFilter("/*")
public class FilterTest implements Filter {
@Override
public void init(FilterConfig fc) throws ServletException{
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
request.setCharacterEncoding("UTF-8");
String method = request.getMethod();
if("GET".equalsIgnoreCase(method)){
String ServerInfo = request.getServletContext().getServerInfo();
String version = ServerInfo.substring(ServerInfo.indexOf("/")+1,ServerInfo.indexOf("."));
if(Integer.parseInt(version) < 8){
HttpServletRequest myRequest = new MyWapper(request);
filterChain.doFilter(myRequest,response);
return ;
}
}
filterChain.doFilter(request,response);
}
@Override
public void destroy() {
}
}
public class MyWapper extends HttpServletRequestWrapper {
private HttpServletRequest req;
public MyWapper(HttpServletRequest request) {
super(request);
this.req = request;
}
@Override
public String getParameter(String name){
String val = req.getParameter(name);
if(val != null && "".equals(val.trim())){
try{
val = new String(val.getBytes("ISO-8859-1"),"UTF-8");
} catch (UnsupportedEncodingException e){
e.printStackTrace();
}
}
return val;
}
}
* 非法访问拦截(即当用户未登录时,拦截需要登录授权才能使用的请求)
* 拦截的资源: 所有,“
public class BlockIllegalAccess implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) servletRequest;
HttpServletResponse res = (HttpServletResponse) servletResponse;
String UrlPath = req.getRequestURI();
if(UrlPath.contains("/Login.jsp") || UrlPath.contains("/Register.jsp")){
filterChain.doFilter(req,res);
return;
}
if(UrlPath.contains("/js") || UrlPath.contains("/css") ||UrlPath.contains("/image")){
filterChain.doFilter(req,res);
return;
}
if(UrlPath.contains("/Login") || UrlPath.contains("/Register") || UrlPath.contains("/ElseOperation")){
filterChain.doFilter(req,res);
return;
}
HttpSession hsr = req.getSession();
String UserStatus = (String) hsr.getAttribute("UserStatus");
if(UserStatus.equals("0")){
res.sendRedirect("Login.jsp");
} else{
filterChain.doFilter(req,res);
return;
}
}
@Override
public void destroy() {
}
}
二,监听器: 1.web监听器是Servlet中一种的特殊的类,能帮助开发者监听web中的特定事件,如: SevletContext,HttpSession, ServletRequest等的创建和销毁,其变量的创建和销毁,修改等, 可以在某些动作前后增加处理,实现监控。 2. 实例:在线人数统计 3.监听器有三类: -监听生命周期: ServletRequestListener HttpSessionListener ServletContextListener
-监听属性的值的变化:
ServletRequestAttributeListener
ServletSessionAttributeListener
ServletContextAttributeListener
-监听Session中的对象:
监听Session中java对象(javaBean),是javaBean直接实现监听器的接口。
@WebListener("/*")
public class ListenerTest implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("Session被创建了");
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("Session被销毁了");
}
}
@WebServlet("/SessionTest")
public class TestServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
HttpSession MySession = req.getSession();
MySession.setAttribute("Name","vodka");
}
}
@WebServlet
public class SessionDestroy extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
req.getSession().invalidate();
}
}
@WebListener
public class DetectedNumbers implements HttpSessionListener {
private int PeopleNumbers = 0;
@Override
public void sessionCreated(HttpSessionEvent hse) {
System.out.println("session已建立");
PeopleNumbers++;
hse.getSession().getServletContext().setAttribute("PeopleNumbers",PeopleNumbers);
}
@Override
public void sessionDestroyed(HttpSessionEvent hse) {
PeopleNumbers--;
hse.getSession().getServletContext().setAttribute("PeopleNumbers",PeopleNumbers);
System.out.println("session被销毁了");
}
}
@WebServlet("/PeopleNum")
public class DetectedServer extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=UTF-8");
HttpSession session = req.getSession();
String key = req.getParameter("key");
if(key != null && "logout".equals(key)){
session.invalidate();
return;
}
ServletContext sc = session.getServletContext();
int nums = (int) sc.getAttribute("PeopleNumbers");
resp.getWriter().write("<h1>当前在线人数为:"+ nums+"</h1> <a href='PeopleNum?key=logout'>退出</a>");
}
}
|