• Filter和ServletContext和Listener


    目录

    Filter案例

    解决全站乱码问题

    登录权限校验

    ServletContext对象

    Listener(监听器)


    Filter案例

    解决全站乱码问题

    我们每次访问每个servlet都要书写处理请求和响应乱码的代码,这样代码十分冗余,所以我们可以在过滤中

    1. @WebFilter("/*")
    2. public class EncodeFilter implements Filter {
    3. @Override
    4. public void init(FilterConfig filterConfig) throws ServletException {
    5. }
    6. @Override
    7. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    8. HttpServletRequest request = (HttpServletRequest) servletRequest;
    9. HttpServletResponse response = (HttpServletResponse) servletResponse;
    10. //your code..
    11. //解决请求乱码
    12. request.setCharacterEncoding("utf-8");
    13. //解决响应乱码
    14. response.setContentType("text/html;charset=utf-8");
    15. //放行
    16. filterChain.doFilter(request,response);
    17. }
    18. @Override
    19. public void destroy() {
    20. }
    21. }
    1. @WebServlet("/servletDemo1")
    2. public class servletDemo1 extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    5. String username = request.getParameter("username");
    6. System.out.println(username);
    7. response.getWriter().print("哈哈哈");
    8. }
    9. @Override
    10. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    11. doGet(request, response);
    12. }
    13. }
    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <form action="/servletDemo1" method="post">
    9. <input type="text" name="username">
    10. <input type="submit" value="提交">
    11. form>
    12. body>
    13. html>

    登录权限校验

    步骤

            1.访问hhh.html这个页面时必须先要登录,不登录不能访问,使用过滤器代码书写代码让其跳转到登录页面login.html

             2.登录loginServlet中获取用户名和密码,存到user对象中,然后存在session中(不关闭服务器,这个session就一直在服务器里面),最后重定向到hhh.html

            3. 使用一个过滤器对hhh.html进行拦截,先从session中获取用户信息,如果没有登录,获取的是null,就跳转到登录页面,如果不为null,说明登录成功,放行

     hhh.html

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>Titletitle>
    6. head>
    7. <body>
    8. <h1>这就是终极文件h1>
    9. body>
    10. html>

    login.html

    1. html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>登录title>
    6. head>
    7. <body>
    8. <form action="/loginServlet" method="post">
    9. 用户名:<input type="text" name="username"><br>
    10. 密码:<input type="password" name="pwd"><br>
    11. <input type="submit" value="提交">
    12. form>
    13. body>
    14. html>

    loginServlet

    1. @WebServlet("/loginServlet")
    2. public class loginServlet extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    5. //获取请求参数
    6. String username = request.getParameter("username");
    7. String pwd = request.getParameter("pwd");
    8. User user = new User();
    9. user.setUsername(username);
    10. user.setPwd(pwd);
    11. //这里在实际开发中,应该调用业务层的方法,与数据库交互
    12. //获取session对象
    13. HttpSession session = request.getSession();
    14. session.setAttribute("u",user);
    15. //登陆成功,就跳转到hhh.html
    16. response.sendRedirect("/hhh.html");
    17. }
    18. @Override
    19. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    20. doGet(request, response);
    21. }
    22. }

    loginFilter过滤器,过滤hhh.html

    1. @WebFilter("/hhh.html")
    2. public class loginFilter implements Filter {
    3. @Override
    4. public void init(FilterConfig filterConfig) throws ServletException {
    5. }
    6. @Override
    7. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    8. HttpServletRequest request = (HttpServletRequest) servletRequest;
    9. HttpServletResponse response = (HttpServletResponse) servletResponse;
    10. //your code..
    11. //从session中获取对象
    12. HttpSession session = request.getSession();
    13. User u = (User) session.getAttribute("u");
    14. if(u==null){
    15. //没有登录
    16. //跳转到登录页面
    17. response.sendRedirect("/login.html");
    18. }else{
    19. //放行
    20. filterChain.doFilter(request,response);
    21. }
    22. }
    23. @Override
    24. public void destroy() {
    25. }
    26. }

    先登录

    在显示页面

    ServletContext对象

    可以共享数据的对象

    1.request:只能在一次请求一次响应中进行数据共享-->请求转发

    2.session:只能在一次会话过程中,可以有多次请求和响应

    3.ServletContext:只要项目存在就可以共享数据,多次会话,多次请求多次响应都可以共享数据,操作整个项目的配置文件

    范围大小:

    ServletContext>session>request

    ServletContext属于接口,在tomcat启动的时候tomcat创建门面类(ServletContext的子类)

    我们可以在Servlet类中直接获取ServletContext对象;getServletContext()属于父类GenericServlet中的方法

    1. @WebServlet("/servletContextDemo1")
    2. public class servletContextDemo1 extends HttpServlet {
    3. @Override
    4. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    5. //获取ServletContext对象
    6. ServletContext servletContext = getServletContext();
    7. //获取指定文件的MIME类型
    8. String mimeType = servletContext.getMimeType("demo1.html");//相对于webapp的路径
    9. System.out.println("mimeType="+mimeType);//mimeType=text/html
    10. //获取指定文件的真实路径
    11. String realPath = servletContext.getRealPath("demo1.html");
    12. System.out.println("realPath="+realPath);
    13. //realPath=D:\java code\web6_filter_listener\target\web6_filter_listener-1.0-SNAPSHOT\demo1.html
    14. }
    15. @Override
    16. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    17. doGet(request, response);
    18. }
    19. }

     

    Listener(监听器

    说明:
             javaweb的监听器就是监听ServletContext HttpServletRequest HttpSession这三个对象的创建和销毁,同时监听是哪个对象数据的变化,就是监听属性的变化:setAttibute(),removeAttribute()

    1.ServletContext是tomcat启动创建,关闭tomcat销毁

    2.HttpServlet是浏览器第一次访问执行request.getSession()创建,销毁时间 1)30min 2)执行invalidate()

    3.HttpServletRequest时浏览器第一次访问Servlet创建,浏览器收到服务器的响应就销毁

    1. //配置监听器
    2. @WebListener
    3. public class conTextListerner implements ServletContextListener {//实现接口
    4. //当ServletContext创建(启动tomcat)时执行该方法
    5. @Override
    6. public void contextInitialized(ServletContextEvent servletContextEvent) {
    7. System.out.println("ServletContext创建");
    8. }
    9. //当ServletContext(关闭tomcat)销毁时执行该方法
    10. @Override
    11. public void contextDestroyed(ServletContextEvent servletContextEvent) {
    12. System.out.println("ServletContext销毁");
    13. }
    14. }

     不使用注解可以在web.xml中配置

    1. <listener>
    2. <listener-class>com.hhh.listener.conTextListernerlistener-class>
    3. listener>

  • 相关阅读:
    刷题经验分享(一)
    打造千万级流量秒杀第二十七课 单元测试:如何做单元测试和 benchmark?
    如何查看多开的逍遥模拟器的adb连接端口号
    基于卷积神经网络的分类算法
    fltp备份文件后统计验证
    [附源码]JAVA毕业设计课程网站设计(系统+LW)
    【学习笔记22】JavaScript数组的练习题
    Redis_09_Redis集群实现Sentinel哨兵应对高可用
    嵌入式养成计划-35------C++绪论------C++数据类型------array容器------命名空间
    【Milvus的以图搜图】
  • 原文地址:https://blog.csdn.net/luosuss/article/details/139336287