zoukankan      html  css  js  c++  java
  • 在eclipse完成对Java_web项目里面资源文件的读取

    Java_web项目的资源文件一般有两种:

    一种是存放数据之间有联系的文件,使用xml文件

    另一种是存放数据之间没有联系的文件,使用properties文件

    这里我们对properties文件读写做示范:

    1、首先在eclipse的src目录下创建一个资源文件properties

    我们可以看到没有创建file文件的选项,那就选Other

    然后点finish就可以了。

    文件里面随便放点数据:

    url=127.0.0.1
    name=root
    password=root

    之后在src的test包里面创建一个ServletContextDemo2.Java

    文件内容如下:

    package test;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class ServletContextDemo2
     */
    @WebServlet("/ServletContextDemo2")    //注意有了这个就不需要往web.xml文件里面添加路径映射
    public class ServletContextDemo2 extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            //路径上第一个斜杠/相对于项目day02的相对路径
            InputStream in = this.getServletContext().getResourceAsStream("/build/classes/db.properties");
            //FileInputStream in = new FileInputStream("classes/db.properties");
            Properties pro = new Properties();
            pro.load(in);  //这个文件的值会以map的形式存放
            
            String url = pro.getProperty("url");
            String name = pro.getProperty("name");
            String password = pro.getProperty("password");
            
            System.out.println(url);
            System.out.println(name);
            System.out.println(password);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }

    要特别注意一下这一条语句:

    InputStream in = this.getServletContext().getResourceAsStream("/build/classes/db.properties");

    这个文件路径可不是你看到的路径,什么意思呢?

    我们在src目录下创建的db.properties,但是我们的项目发布之后是没有src这个目录的。所以你可不能把路径写成src/db.properties

    那么我们怎么看自己项目发布之后的路径呢?

    打开项目属性:

    到这里大家应该知道了为什么路径要写成

    /build/classes/db.properties  东西了吧!

    如果你想要在一个Java包里面创建资源文件,之后访问。那就把路径改成

    /build/classes/包/db.properties

    注意:

    如果上面的不行,那就把文件地址换成

    /WEB-INF/classes/包/db.properties

    也就是把build都换成WEB-INF

    不知道怎么回事,之前build使用谷歌浏览器可以,但是又不行了(我就搞了搞其他东西,又回来试了试,就不行了)。

    换成WEB-INF之后,你使用谷歌浏览器访问可能会抛出

    java.lang.NullPointerException
        java.util.Properties$LineReader.readLine(Properties.java:434)
        java.util.Properties.load0(Properties.java:353)
        java.util.Properties.load(Properties.java:341)
        test.ServletContextDemo2.doGet(ServletContextDemo2.java:86)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
        javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
        org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

    但是换成IE浏览器(window10自带)可以。我算是服了

    不知道谷歌浏览器怎么回事,一脸懵

    好像是不能在原网页上刷新,你可以试一试重新打开一个页面

     ---------------------------------------------------------分隔------------------------------------------------

    你可以通过类装载器方式读取资源文件,这样是没有问题的,IE、谷歌都可以 ,具体见:

    Java_web项目中在Java文件里面通过类装载器对资源文件读取

    你也可以得到文件发布之后的绝对路径,这样的话还可以得到文件名

    改一下ServletContext.java文件内容

    //package test;
    //
    //import java.io.FileInputStream;
    //import java.io.IOException;
    //import java.io.InputStream;
    //import java.util.Properties;
    //
    //import javax.servlet.ServletException;
    //import javax.servlet.annotation.WebServlet;
    //import javax.servlet.http.HttpServlet;
    //import javax.servlet.http.HttpServletRequest;
    //import javax.servlet.http.HttpServletResponse;
    //
    ///**
    // * Servlet implementation class ServletContextDemo2
    // */
    //@WebServlet("/ServletContextDemo2")    //注意有了这个就不需要往web.xml文件里面添加路径映射
    //public class ServletContextDemo2 extends HttpServlet {
    //    private static final long serialVersionUID = 1L;
    //
    //    /**
    //     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    //     */
    //    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //        // TODO Auto-generated method stub
    //        //路径上第一个斜杠/相对于项目day02的相对路径
    ////        String path = this.getServletContext().getRealPath("/build/classes/db.properties");
    ////        FileInputStream in = new FileInputStream(path);
    ////        //文件路径
    ////        System.out.println(path);
    ////        String file_name = path.substring(path.lastIndexOf("\")+1);
    ////        //文件名
    ////        System.out.println(file_name);
    //        
    //        InputStream in = this.getServletContext().getResourceAsStream("/build/classes/db.properties");
    //        Properties pro = new Properties();
    //        pro.load(in);  //这个文件的值会以map的形式存放
    //        
    //        String url = pro.getProperty("url");
    //        String name = pro.getProperty("name");
    //        String password = pro.getProperty("password");
    //        
    //        System.out.println(url);
    //        System.out.println(name);
    //        System.out.println(password);
    //    }
    //
    //    /**
    //     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    //     */
    //    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //        // TODO Auto-generated method stub
    //        doGet(request, response);
    //    }
    //
    //}
    package test;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;
    
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    /**
     * Servlet implementation class ServletContextDemo2
     */
    @WebServlet("/ServletContextDemo2")    //注意有了这个就不需要往web.xml文件里面添加路径映射
    public class ServletContextDemo2 extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        /**
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            //路径上第一个斜杠/相对于项目day02的相对路径
            String path = this.getServletContext().getRealPath("/build/classes/db.properties");
            FileInputStream in = new FileInputStream(path);
            Properties pro = new Properties();
            pro.load(in);  //这个文件的值会以map的形式存放
            //文件路径
            System.out.println(path);
            //文件名
            String file_name = path.substring(path.lastIndexOf("\")+1);
            System.out.println(file_name);
            
            String url = pro.getProperty("url");
            String name = pro.getProperty("name");
            String password = pro.getProperty("password");
            
            System.out.println(url);
            System.out.println(name);
            System.out.println(password);
        }
    
        /**
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
         */
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // TODO Auto-generated method stub
            doGet(request, response);
        }
    
    }

     

  • 相关阅读:
    P1308 统计单词数(cin,getline() ,transform() )
    解决ASP.NET中的各种乱码问题
    GUID
    c# Thread、ThreadPool、Task的区别
    线程学习参考
    异步
    Lamda简单使用
    ubuntu上安装docker
    Git设置ssh密钥
    Git客户端(TortoiseGit)基本使用详解
  • 原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/14088681.html
Copyright © 2011-2022 走看看