一。web监听器:监听特殊事件的发生
1.监听实现步骤
a。写一个java类,实现特定的接口,重写相关方法
b。在web.xml中,牌配置
<listener>
<listener-class>com.listener.MyContextLisenter</listener-class>
</listener>
c.注解方式:在java类上写一个@WebLisenter
2.可以在web.xml中可以配置servetContext初始化的参数
<context-param>
<param-name>basePath</param-name>
<param-value>http://localhost:8888/StudentManager/</param-value>
</context-param>
在程序中获取方式:sce.getServletContext().getInitParameter("basePath");
3.三种监听:请求监听(ServletRequestListener),会话监听(HttpSessionListener),上下文监听(ServletContextListener)
二。过滤器
1、特点:在服务器中第一个被执行,可以拦截任意指定的路径请求
2.用于项目中的非法访问,字符集设置等等
3.实现步骤:
a.写一个java程序,实现Filter接口,重写方法
b.在web.xml中配置
<filter>
<filter-name>charfilter</filter-name>
<filter-class>com.filter.CharSetFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>charfilter</filter-name>
<!-- 拦截根目录下的所有请求 -->
<url-pattern>/*</url-pattern>
</filter-mapping>
c.dofilter方法里面的最后一句,应该将请求继续放行china.doFilter(req,resp);
web.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>javaweb06</display-name> <!-- <listener> --> <!-- <listener-class>com.listener.MyRequestListener</listener-class> --> <!-- </listener> --> <context-param> <param-name>basePath</param-name> <param-value>http://localhost:8888/StudentManager/</param-value> </context-param> <filter> <filter-name>charset</filter-name> <filter-class>com.filter.CharSetFilter</filter-class> </filter> <filter-mapping> <filter-name>charset</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>loginservlet</servlet-name> <servlet-class>com.control.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>loginservlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet> <servlet-name>loginservlet2</servlet-name> <servlet-class>com.control.LoginServlet2</servlet-class> </servlet> <servlet-mapping> <servlet-name>loginservlet2</servlet-name> <url-pattern>/login2</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> </web-app>
实例1:
package com.listener;
import javax.servlet.ServletRequestEvent;
import javax.servlet.ServletRequestListener;
public class MyRequestListener implements ServletRequestListener{
@Override
public void requestInitialized(ServletRequestEvent sre) {
sre.getServletContext();
sre.getServletRequest();
System.out.println("请求开始");
}
@Override
public void requestDestroyed(ServletRequestEvent sre) {
System.out.println("请求结束");
}
}
实例2:
package com.listener;
import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class MySessionListener implements HttpSessionListener{
@Override
public void sessionCreated(HttpSessionEvent se) {
String path = se.getSession().getServletContext().getInitParameter("basePath");
System.out.println("初始化参数为:"+path);
ServletContext app = se.getSession().getServletContext();
Integer online = (Integer)app.getAttribute("online");
if(online==null){
online = 0;
}
System.out.println("当前在线人数是:"+ ++online);
app.setAttribute("online", online);
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext app = se.getSession().getServletContext();
Integer online = (Integer)app.getAttribute("online");
System.out.println("当前在线人数为:"+ --online);
app.setAttribute("online", online);
}
}
实例3:
package com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebFilter("/student/*")
public class IsLoginFilter implements Filter{
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain arg2)
throws IOException, ServletException {
//1.从session中获取用户信息
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
String username = (String) request.getSession().getAttribute("username");
System.out.println(request.getRequestURI());
if(request.getRequestURI().endsWith("/login.jsp")||request.getRequestURI().endsWith("/login3")){
arg2.doFilter(req, resp);
return;
}
if(username==null){//非法登录
response.sendRedirect("login.jsp");
}else{
arg2.doFilter(req, resp);
}
}
}
实例4:
package com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
/**
* 将所有请求的字符集统一修改
* @author allen
*/
@WebFilter("/*")
public class CharSetFilter implements Filter{
@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain arg2)
throws IOException, ServletException {
System.out.println("请求字符集修改为utf-8");
req.setCharacterEncoding("utf-8");
arg2.doFilter(req,resp);//将请求继续往下放行
}
}