zoukankan      html  css  js  c++  java
  • web访问资源

    web根据资源名称去到class目录找到对应的文件,output输出屏幕

    POM添加依赖

            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-core</artifactId>
                <version>8.5.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-el</artifactId>
                <version>8.5.14</version>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <version>8.5.5</version>
            </dependency>
    

    配置资源拦截器,同时classesMETA-INFservices 目录下新建文件javax.servlet.ServletContainerInitializer 配置:xyz.luofu.www.app.MyServletContainerInitializer(tomcat会加载这里的文件)

    public class MyServletContainerInitializer implements ServletContainerInitializer {
        @Override
        public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
            ServletRegistration.Dynamic myServletContainerInitializer =
                ctx.addServlet("myServletDispatcher", MyServletDispatcher.class);
            myServletContainerInitializer.addMapping("/");
            System.out.println(">>>>>>>>>>>>>> myServletDispatcher init");
        }
    }
    

    编写的tomcat,同时加载webapp

    public class MySpringApplication {
    
        public void run() {
    
            Tomcat tomcat = new Tomcat();
            tomcat.setPort(9090);
            try {
                String sourcePath = MySpringApplication.class.getResource("/").getPath();
                Context context = tomcat.addWebapp("/",new File("demo/src/main/webapp").getAbsolutePath());
                WebResourceRoot webResourceRoot = new StandardRoot(context);
                webResourceRoot.addPreResources(new DirResourceSet(webResourceRoot, "/WEB-INF/classes", sourcePath, "/"));
                tomcat.start();
                tomcat.getServer().await();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    }
    

    处理输出

    public class MyServletDispatcher extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
            String requestURI = req.getRequestURI();
    
            String classPath = MySpringApplication.class.getResource("/").getPath();
            System.out.println(requestURI + ">>>>>" + classPath);
            String path = classPath + requestURI;
            FileInputStream is = new FileInputStream(new File(path));
            byte[] bytes = new byte[1024];
            is.read(bytes);
            is.close();
            resp.setContentType("text/html");
            System.out.println("++++++++++++++++++"+new String(bytes));
            resp.getWriter().write(new String(bytes)+"jjj");
        }
    }
    

    test类

    public class Test {
        public static void main(String[] args) {
            new MySpringApplication().run();
        }
    }
    

    浏览器访问index.html(classes根目录下的文件) http://localhost:9090/index.html 可以成功显示

  • 相关阅读:
    CSPS_108
    TortoiseGit配置密钥的方法
    SLF4J和Logback和Log4j和Logging的区别与联系
    slf4j log4j logback关系详解和相关用法
    dubbo服务telnet命令的使用
    基于IDEA的JavaWeb开发环境搭建
    jdk8--十大新特性
    jdk8--collect总结
    字符串的排列
    调整数组顺序使奇数位于偶数前面
  • 原文地址:https://www.cnblogs.com/leifonlyone/p/12781709.html
Copyright © 2011-2022 走看看