免费中文网站模板下载,河南建设工程招标网站,茌平网站建设电话,车辆管理网站开发文章目录 Web组件Listener监听器ServletContextListener执行过程 Filter过滤器Filter与Servlet的执行 案例#xff08;登录案例#xff09; 小结Web组件 Web组件
JavaEE的Web组件#xff08;三大Web组件#xff09;#xff1a; Servlet → 处理请求对应的业务Listener →… 文章目录 Web组件Listener监听器ServletContextListener执行过程 Filter过滤器Filter与Servlet的执行 案例登录案例 小结Web组件 Web组件
JavaEE的Web组件三大Web组件 Servlet → 处理请求对应的业务Listener → 监听器Filter → 过滤器 Listener监听器
监听器在监听到主体做了XX事情就会触发对应的事件监听的东西与命名有关系要加上注解WebListener
ServletContextListener
监听的主体就是ServletContext当发现ServletContext做了事情Listener监听器就会执行该事件特定的方法。
ServletContext如果初始化则会执行监听器的初始化方法contextInitialized ServletContext应用程序启动的时候初始化就意味着应用程序启动 ServletContext如果销毁则会执行监听器的销毁方法contextDestroy ServletContext应用程序关闭的时候销毁意味着应用程序关闭 应用程序启动的时候会执行ServletContextListener的contextInitialized方法应用程序关闭的时候会执行contextDestroy
执行过程
当应用程序启动的过程中逐步加载Web组件
首先会加载ServletContext和Listener组件 ServletContext伴随着应用程序初始化它开始初始化然后ServletContextListener监听到ServletContext初始化会执行Listener的Initialized方法 然后初始化loadOnStartup为正数的Servlet eg
WebListener
public class PropertyInitServletContextListener implements ServletContextListener {SneakyThrowsOverridepublic void contextInitialized(ServletContextEvent servletContextEvent) {ServletContext servletContext servletContextEvent.getServletContext();InputStream inputStream PropertyInitServletContextListener.class.getClassLoader().getResourceAsStream(parameter.properties);Properties properties new Properties();properties.load(inputStream);String picPath properties.getProperty(pic.path);servletContext.setAttribute(picPath, picPath);}Overridepublic void contextDestroyed(ServletContextEvent servletContextEvent) {System.out.println(应用程序关闭可以做一些资源的释放);}
}
/*** localhost:8080/demo1/picture/fetch?name1.jpg*/
WebServlet(/picture/fetch)
public class PictureAccessServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {// InputStream inputStream PictureAccessServlet.class.getClassLoader()// .getResourceAsStream(parameter.properties);// Properties properties new Properties();// properties.load(inputStream);// String path properties.getProperty(pic.path);String path (String) getServletContext().getAttribute(picPath);File file new File(path,request.getParameter(name));FileInputStream fileInputStream new FileInputStream(file);ServletOutputStream outputStream response.getOutputStream();int length 0;byte[] bytes new byte[1024];while((length fileInputStream.read(bytes)) ! -1) {outputStream.write(bytes,0,length);}fileInputStream.close();outputStream.close();}
} Filter过滤器
Filter是一个执行过滤任务的一个对象 它既可以作用于Request对象也可以作用于Response对象或者两者均作用 就是Servlet中获取请求之前Servlet响应之后 Filter与Servlet的执行
URL-Pattern和Servlet之间存在着映射关系URL-Pattern和Filter之间也存在着映射关系 1个URL-Pattern只能对应一个Servlet但是可以对应多个FilterServlet和URL-Pattern之间是一对多的关系但是URL-Pattern和Servlet之间是一对一 其实一个URL-Pattern对应的请求 对应1个Servlet对应多个Filter eg
WebFilter(/user/*)
public class CharacterEncodingFilter implements Filter {Overridepublic void init(FilterConfig filterConfig) throws ServletException {ServletContext servletContext filterConfig.getServletContext();}Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)throws IOException, ServletException {// 做过滤里面写的就是通用的业务// 前半部分// 这部分是解决中文乱码问题HttpServletResponse response (HttpServletResponse) servletResponse;HttpServletRequest request (HttpServletRequest) servletRequest;request.setCharacterEncoding(utf-8);response.setContentType(text/html;charsetutf-8);filterChain.doFilter(servletRequest, servletResponse); // 执行下一个过滤器或者Servlet// 后半部分// 但是一般业务代码都写在前半部分}Overridepublic void destroy() {}
} 案例登录案例
需求 /user/login/user/logout/user/info/order/list增加web组件中的filter功能增加web组件中的listener功能增加白名单功能 bean目录下
Data
public class Order {private Integer id;private String name;private Double price;private Integer userId;
}Data
public class User {private Integer id;private String username;private String password;private Integer age;private Date birthday;private Date createdate;private String mobile;}mapper目录下
public interface OrderMapper {ListOrder selectByUserId(Integer id);
}public interface UserMapper {ListUser selectByUserNameAndPassword(Param(username) String username, Param(password) String password);User selectByPrimaryKey(Integer id);
}servlet目录下
WebServlet(/order/*)
public class OrderServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {process(req, resp);}Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {process(req, resp);}SneakyThrowsprivate void process(HttpServletRequest request, HttpServletResponse response) {DispatchUtil.dispatch(request,response,this);}SneakyThrowsprivate void list(HttpServletRequest request, HttpServletResponse response){// 先判断是否是登录状态// 如果没有登录则跳转到登录页面// 这里filter帮我们完成了// 如果已经登录则查询orderlist信息OrderMapper orderMapper MybatisUtil.getSqlSession().getMapper(OrderMapper.class);HttpSession session request.getSession();User user (User) session.getAttribute(user);ListOrder orders orderMapper.selectByUserId(user.getId());response.getWriter().println(orders);}
}WebServlet(/user/*)
public class UserServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {process(req, resp);}Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {process(req, resp);}SneakyThrowsprivate void process(HttpServletRequest request, HttpServletResponse response) {DispatchUtil.dispatch(request, response, this);}SneakyThrowsprivate void login(HttpServletRequest request, HttpServletResponse response){// 获取username和password查询user记录String username request.getParameter(username);String password request.getParameter(password);UserMapper userMapper MybatisUtil.getSqlSession().getMapper(UserMapper.class);ListUser users userMapper.selectByUserNameAndPassword(username, password);// 有可能用户名和密码相同则取第一条数据if (users ! null users.size() 0) {User user users.get(0);HttpSession session request.getSession();session.setAttribute(user,user);response.getWriter().println(登录成功);} else {response.getWriter().println(用户名或密码错误即将跳转到登录页面...);response.setHeader(refresh,2;url/demo3/login.html);}}SneakyThrowsprivate void logout(HttpServletRequest request, HttpServletResponse response){HttpSession session request.getSession();session.invalidate();response.getWriter().println(注销用户);}SneakyThrowsprivate void info(HttpServletRequest request, HttpServletResponse response){// 先判断是否是登录状态// 如果没有登录则跳转到登录页面// 这里filter帮我们做了// 如果已经登录可以从session中获取信息 - 响应信息HttpSession session request.getSession();User user (User) session.getAttribute(user);response.getWriter().println(user);}
}listener目录下
WebListener
public class PropertyServletContextListener implements ServletContextListener {Overridepublic void contextInitialized(ServletContextEvent servletContextEvent) {ServletContext servletContext servletContextEvent.getServletContext();Properties properties new Properties();InputStream inputStream PropertyServletContextListener.class.getClassLoader().getResourceAsStream(application.properties);try {properties.load(inputStream);} catch (IOException e) {e.printStackTrace();}String whiteList properties.getProperty(whiteList);String[] whiteListStr whiteList.split(,);ListString list Arrays.asList(whiteListStr);servletContext.setAttribute(whiteList, list);}Overridepublic void contextDestroyed(ServletContextEvent sce) {}
}filter目录下
WebFilter(/*)
public class CharacterEncodingFilter implements Filter {ListString whiteList null;SneakyThrowsOverridepublic void init(FilterConfig filterConfig) throws ServletException {// 方法1
// Properties properties new Properties();
// InputStream inputStream CharacterEncodingFilter.class.getClassLoader()
// .getResourceAsStream(application.properties);
// properties.load(inputStream);
// String whiteListStr properties.getProperty(whiteList);
// String[] whiteArray whiteListStr.split(,);
// whiteList Arrays.asList(whiteArray);// 方法2// 可以先把配置文件在Listener的时候放入whiteList (ListString) filterConfig.getServletContext().getAttribute(whiteList);}Overridepublic void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)throws IOException, ServletException {// ListString list Arrays.asList(/user/login, /user/logout, /login.html);// 强转HttpServletRequest request (HttpServletRequest) servletRequest;HttpServletResponse response (HttpServletResponse) servletResponse;String uriSuffix UriUtil.getUriSuffix(request);if (whiteList.contains(uriSuffix)) {if (!uriSuffix.endsWith(html)) {// 1. 解决字符集request.setCharacterEncoding(utf-8);// 响应的字符集问题response.setContentType(text/html;charsetutf-8);}filterChain.doFilter(request, response);}// 1. 解决字符集request.setCharacterEncoding(utf-8);// 响应的字符集问题response.setContentType(text/html;charsetutf-8);// 2. 解决登录状态的判断HttpSession session request.getSession();Object user session.getAttribute(user);// 2.1 如果已经登录或者登录/注销请求就放行if (user ! null) {filterChain.doFilter(request, response);} else {// 2.2 如果没有登录就提示跳转到登录页面response.getWriter().println(没有登录请先登录即将跳转到登录页面...);response.setHeader(refresh, 2;url/demo3/login.html);}// String requestURI request.getRequestURI();// if (requestURI.endsWith(html)) {
// filterChain.doFilter(request, response);
// }// 2.1 如果已经登录或者登录/注销请求就放行
// if (user ! null || requestURI.endsWith(login)
// || requestURI.endsWith(logout)){
// filterChain.doFilter(request, response);
// } else {
// // 2.2 如果没有登录就提示跳转到登录页面
// response.getWriter().println(没有登录请先登录即将跳转到登录页面...);
// response.setHeader(refresh,2;url/demo3/login.html);
// }}Overridepublic void destroy() {}
}util目录下
public class DispatchUtil {public static void dispatch(String operation, HttpServletRequest request, HttpServletResponse response, HttpServlet instance)throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {Method method instance.getClass().getDeclaredMethod(operation, HttpServletRequest.class, HttpServletResponse.class);method.setAccessible(true);method.invoke(instance, new Object[]{request, response});}public static void dispatch(HttpServletRequest request, HttpServletResponse response, HttpServlet userServlet)throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {String requestURI request.getRequestURI();String operation requestURI.substring(requestURI.lastIndexOf(/) 1);dispatch(operation, request, response, userServlet);}
}public class MybatisUtil {private static SqlSession sqlSession;static{try {sqlSession new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream(mybatis.xml)).openSession(true);} catch (IOException e) {e.printStackTrace();}}public static SqlSession getSqlSession() {return sqlSession;}
}public class UriUtil {// Suffix的意思是后缀public static String getUriSuffix(HttpServletRequest request) {// uri - contextPathString uriSuffix null;// eg: /demo3/user/nameString requestURI request.getRequestURI();// eg: /demo3String contextPath request.getContextPath();if (contextPath null || contextPath.length() 0) {// 这是一个root应用uriSuffix requestURI;} else {uriSuffix requestURI.replaceAll(contextPath, );// 或者uriSuffix requestURI.substring();}return uriSuffix;}
}UserMapper的xml目录
mapper namespacecom.coo1heisenberg.demo3.mapper.OrderMapperselect idselectByUserId resultTypecom.coo1heisenberg.demo3.bean.Orderselect id, name, price, user_id as userId from test_productwhereuser_id #{user_id}/where/select
/mappermapper namespacecom.coo1heisenberg.demo3.mapper.UserMapperselect idselectByUserNameAndPassword resultTypecom.coo1heisenberg.demo3.bean.Userselect id, username, password, age, birthday, createDate, mobile from test_userwhereusername #{username} and password #{password}/where/selectselect idselectByPrimaryKey resultTypecom.coo1heisenberg.demo3.bean.Userselect id, username, password, age, birthday, createDate, mobile from test_userwhereid #{id}/where/select
/mapper小结
Web组件
核心是Servlet处理核心业务Listener用来做资源的初始化Filter在Servlet处理前后增加通用的处理