Servlet3.0 注解版实例和三大组件

Shared libraries(共享库)/runtimes pluggability(运行时插件能力)
1、Servlet容器启动会扫描,当前应用里面每一个jar包的ServletContainerInitializer的实现
2、提供Servlet Container Initializer的实现类:
	必须绑定在,META-INF/services/javax.servlet.ServletContainerInitializer
	文件的内容就是ServletContainerInitializer实现类的全类名;

实例版:工程的目录
Servlet3.0 注解版实例和三大组件_第1张图片
servlet类

package com.haha;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/hello")
public class Servler extends HttpServlet {
     
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     
		// TODO Auto-generated method stub
		//super.doGet(req, resp);
		resp.getWriter().write("hello!");
	}
}

输出的结果:
Servlet3.0 注解版实例和三大组件_第2张图片

总结:容器在启动应用的时候,会扫描当前应用每一个jar包里面
	META-INF/services/javax.servlet.ServletContainerInitializer
	指定的实现类,启动并运行这个实现类的方法;

servlet的三大组件
利用ServletContext组测文本组件(servlet、Filter、Listener)
在其中定义一个接口Service的实现类abstractHelloService、HelloServiceExt
、HelloServiceImp

package com.haha;

import java.util.EnumSet;
import java.util.Set;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.annotation.HandlesTypes;
import javax.xml.ws.Dispatch;
//容器一启动就会把HelloService传递过来
//容器已启动的时候会将@HandlesTypes指定的这个类型下面的子类(实现类、子接口等)传递过来;
@HandlesTypes(value={
     HelloService.class})
public class MyServletContainerInitializer implements ServletContainerInitializer {
     
	/**
	 * onStartup 应用一启动的时候就回运行onStartup这个方法
	 * Set> arg0  这个是传入感兴趣的类型就像是刚刚传入的HelloService接口一样
	 * ServletContext arg1  用来代表一个web应用,一个web代表着一个应用
	 * 
	 * 利用ServletContext组测文本组件(servlet、Filter、Listener)
	 */
	@Override
	public void onStartup(Set<Class<?>> arg0, ServletContext arg1) throws ServletException {
     
		// TODO Auto-generated method stub
		System.out.println("感兴趣的类型:");
		for (Class<?> cl : arg0) {
     
			System.out.println(cl);
		}
		//注册一个servlet
		Dynamic s = arg1.addServlet("userSsrvlet", new UserServlet());
		//添加servlet的映射信息
		s.addMapping("/user");
		//注册一个过滤器
		FilterRegistration.Dynamic d = arg1.addFilter("userFilter", UserFilter.class);
		d.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
		//注册一个监听器
		arg1.addListener(UserListener.class);
	}

}

servlet

package com.haha;

import java.io.IOException;

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

public class UserServlet extends HttpServlet {
     
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     
		// TODO Auto-generated method stub
		resp.getWriter().write("tomcat.....");
	}
}

Filter

package com.haha;

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;

public class UserFilter implements Filter{
     

	@Override
	public void destroy() {
     
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
			throws IOException, ServletException {
     
		// 实现过滤
		System.out.println("使用过滤器。。。");
		arg2.doFilter(arg0, arg1);
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
     
		// TODO Auto-generated method stub
		
	}

}

、Listener

package com.haha;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class UserListener implements ServletContextListener {
     

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
     
		// TODO Auto-generated method stub
		System.out.println("contextDestroyed()被执行了。。。。");
		
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
     
		// TODO Auto-generated method stub
		System.out.println("contextInitialized()被执行了。。。。");
		
	}

}

Servlet3.0 注解版实例和三大组件_第3张图片

你可能感兴趣的:(Servlet3.0 注解版实例和三大组件)