• 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 可以成功显示

  • 相关阅读:
    java开发——Cloneable接口、clone()方法和深浅拷贝
    intellij idea 显示Arraylist 扩容过程 解决not showing null elements
    ArrayList的扩容方式和扩容时机
    【laravel5.4】发送alisms短信和163邮箱
    【laravel5.4】重定向带参数
    【laravel5.4】vue分页删除
    【laravel5.4】Baum无限极分类和collect助手函数、transform()中间件(转换数据)方法使用
    【laravel5.4】使用baum ode 类库实现无限极分类
    【VUE+laravel5.4】vue给http请求 添加请求头数据
    【laravel5.4】关键字【use】使用
  • 原文地址:https://www.cnblogs.com/leifonlyone/p/12781709.html
Copyright © 2020-2023  润新知