zoukankan      html  css  js  c++  java
  • Web应用的路径问题

    1.总结:只有转发不用写项目的路径,其他都要!

    /**
     * web应用中路径问题
     * @author APPle
     *
     */
    public class PathDemo extends HttpServlet {
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		response.setContentType("text/html;charset=utf-8");
    		//目标资源: target.html
    		/**
    		 * 思考: 目标资源是给谁使用的。
    		 * 		给服务器使用的:   / 表示在当前web应用的根目录(webRoot下)
    		 * 		给浏览器使用的: /  表示在webapps的根目录下,必须要加上项目路径,即 /day09= this.getServletContext().getContextPath();
    		 */
    		/**
    		 * 1.转发,服务器用
    		 */
    		request.getRequestDispatcher("/target.html").forward(request, response);
    		
    		
    		/**
    		 * 2.请求重定向,浏览器用
    		 */
    		response.sendRedirect("/day11/target.html");
    		
    		/**
    		 * 3.html页面的超连接href  浏览器用
    		 */
    		response.getWriter().write("<html><body><a href='/day11/target.html'>超链接</a></body></html>");
    		
    		/**
    		 * 4.html页面中的form提交地址  浏览器用
    		 */
    		response.getWriter().write("<html><body><form action='/day11/target.html'><input type='submit'/></form></body></html>");
    	}
    
    }
    

      2.路径二: 假如db.properties在src目录下,或者与src平行的resource folder里面的db.propertites也是一样

    /**
     * 读取web应用下的资源文件(例如properties)
     * @author APPle
     */
    public class ResourceDemo extends HttpServlet {//读取web应用下的资源文件(例如properties)
    
    	public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    		/**
    		 *  . 代表java命令运行目录。java运行命令在哪里?? 在tomcat/bin目录下
    		 *   结论: 在web项目中, . 代表在tomcat/bin目录下开始,所以不能使用这种相对路径。
    		 */		
    		//读取文件。在web项目下不要这样读取。因为.表示在tomcat/bin目录下
    		/*File file = new File("./src/db.properties");
    		FileInputStream in = new FileInputStream(file);*/	
    		/**
    		 * 使用web应用下加载资源文件的方法
    		 */
    		/**
    		 * 1. getRealPath读取,返回资源文件的绝对路径  假如db.properties在src目录下
    		 */
    		/*String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");
    		System.out.println(path);
    		File file = new File(path);
    		FileInputStream in = new FileInputStream(file);*/
    		
    		/**
    		 * 2. getResourceAsStream() 得到资源文件,返回的是输入流
    		 */
    		InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
    
    		Properties prop = new Properties();
    		//读取资源文件
    		prop.load(in);
    		
    		String user = prop.getProperty("user");
    		String password = prop.getProperty("password");
    		System.out.println("user="+user);
    		System.out.println("password="+password);
    		
    	}
    
    }
    

      

  • 相关阅读:
    数据结构与算法(一)--数组
    Lucene学习
    java虚拟机面试题(JVM)
    Java开发面试题归类( 题目篇)
    java虚拟机学习(六)
    java虚拟机学习(五)--垃圾收集器总结
    21_异常_第21天(异常、企业面试题,思维导图下载)
    20_集合_第20天(Map、可变参数、Collections)
    19_集合_第19天(List、Set)
    18_集合框架_第18天(集合、Iterator迭代器、增强for循环 、泛型)
  • 原文地址:https://www.cnblogs.com/bravolove/p/5879865.html
Copyright © 2011-2022 走看看