重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本文源码:GitHub·点这里 || GitEE·点这里
疏勒网站建设公司创新互联,疏勒网站设计制作,有大型网站制作公司丰富经验。已为疏勒近千家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的疏勒做网站的公司定做!
JavaWeb三大组件:Servlet,Listener,Filter。监听器就是指在应用程序中监听相关对象状态变化的组件。
指被监听对象。
ServletContextListener
生命周期监听,它有两个方法,出生时调用contextInitialized()
,销毁时调用contextDestroyed()
;
ServletContextAttributeListener
属性监听,它有三个方法,添加属性attributeAdded()
,替换属性attributeReplaced()
,移除属性时attributeRemoved()
。
HttpSessionListener
生命周期监听:它有两个方法,出生时调用sessionCreated()
,销毁时调用sessionDestroyed()
;
HttpSessioniAttributeListener
属性监听:它有三个方法,添加属性attributeAdded()
,替换属性attributeReplaced()
,移除属性attributeRemoved()
。
ServletRequestListener
生命周期监听:它有两个方法,出生时调用requestInitialized()
,销毁时调用requestDestroyed()
;
ServletRequestAttributeListener
属性监听:它有三个方法,添加属性attributeAdded()
,替换属性attributeReplaced()
,移除属性attributeRemoved()
。
TheContextListener
public class TheContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("初始化:TheContextListener");
ServletContext servletContext = servletContextEvent.getServletContext() ;
servletContext.setAttribute("author","cicada");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
System.out.println("销毁:TheContextListener");
}
}
TheRequestListener
public class TheRequestListener implements ServletRequestListener {
@Override
public void requestDestroyed(ServletRequestEvent servletRequestEvent) {
System.out.println("初始化:TheRequestListener");
}
@Override
public void requestInitialized(ServletRequestEvent servletRequestEvent) {
System.out.println("销毁:TheRequestListener");
}
}
TheSessionListener
public class TheSessionListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
System.out.println("初始化:TheSessionListener");
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
System.out.println("销毁:TheSessionListener");
}
}
RequestAttributeListener
public class RequestAttributeListener implements ServletRequestAttributeListener {
@Override
public void attributeAdded(ServletRequestAttributeEvent evt) {
System.out.println("Request添加属性:"+evt.getName()+";"+evt.getValue());
}
@Override
public void attributeRemoved(ServletRequestAttributeEvent evt) {
System.out.println("Request移除属性:"+evt.getName()+";"+evt.getValue());
}
@Override
public void attributeReplaced(ServletRequestAttributeEvent evt) {
System.out.println("Request替换属性:"+evt.getName()+";"+evt.getValue());
}
}
com.node05.servlet.listener.TheContextListener
com.node05.servlet.listener.TheSessionListener
com.node05.servlet.listener.TheRequestListener
com.node05.servlet.listener.RequestAttributeListener
1
public class ListenerServletImpl extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
// 1、获取TheContextListener初始化数据
ServletContext servletContext = this.getServletContext() ;
String author = String.valueOf(servletContext.getAttribute("author")) ;
System.out.println("TheContextListener Author:"+author);
// 2、Request属性设置
request.setAttribute("mood","smile");
request.setAttribute("mood","agitated");
// 3、Session创建,1分钟失效,调用销毁
HttpSession session = request.getSession(true) ;
session.setAttribute("casually","casually");
response.getWriter().print("Hello:Listener");
}
}
客户端请求Servlet时,先执行相关Filter,如果Filter通过,则继承执行请求的Servlet;如果Filter不通过,则不会执行用户请求的Servlet。过滤器可以动态地拦截请求和响应。
Filter接口定义了三个核心方法。
应用程序启动时,服务器实例化Filter对象,并调用其init方法,读取web.xml配置,完成对象的初始化加载。
实际的过滤操作,请求达到服务器时,Servlet容器将先调用过滤器的doFilter方法。
容器在销毁过滤器前调用该方法,释放过滤器占用的资源。
public class ThePrintLogFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String myName = filterConfig.getInitParameter("myName") ;
System.out.println("myName:"+myName);
}
@Override
public void doFilter(ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest)servletRequest ;
HttpServletResponse response = (HttpServletResponse)servletResponse ;
String name = request.getParameter("name") ;
if (!name.equals("cicada")){
response.getWriter().print("User Error !");
return ;
}
chain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
System.out.println("ThePrintLogFilter destroy()");
}
}
thePrintLogFilter
com.node05.servlet.filter.ThePrintLogFilter
myName
cicada
thePrintLogFilter
/filterServletImpl
public class FilterServletImpl extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("Hello:Filter");
}
}
Spring框架中的拦截器Interceptor类似于Servlet中的过滤器Filter,主要用于拦截用户请求并作相应的处理。例如通过拦截器可以进行权限验证、记录请求信息的日志、判断用户是否登录等。请求转发不执行拦截、过滤;重定向执行拦截和过滤。
GitHub·地址
https://github.com/cicadasmile/java-base-parent
GitEE·地址
https://gitee.com/cicadasmile/java-base-parent
创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。