码迷,mamicode.com
首页 > 其他好文 > 详细

ServletContext对象读取资源文件以及重定向

时间:2019-06-14 22:17:05      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:输出流   磁盘   iter   org   inf   read   path   cep   控制台   

有两种方式可以读取资源文件

  * InputStream getResourceAsStream(String path)     通过文件的地址获取输入流

  * String getRealPath(String path)        通过文件的地址获取文件的绝对磁盘路径

步骤:

  1. 创建配置文件db.properties

  2. 获取资源的输出流

  3. 把输出流加载到配置文件中

db.properties

username=root33
password=12333
desc=haha33
package cn.itcast.context;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 读取资源文件
 * @author Administrator
 *
 */
public class ReadServlet extends HttpServlet {
    
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        read5();
    }
    
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }
    
    /**
     * 通过ServletContext对象获取文件的绝对磁盘路径
     * 获取src目录下文件
     * @throws IOException 
     */
    public void read5() throws IOException{
        // 获取对象
        String path = getServletContext().getRealPath("/WEB-INF/classes/db.properties");
        // System.out.println(path);
        // C:\apache-tomcat-6.0.14\webapps\day09\WEB-INF\classes\db.properties
        
        // 获取输入流
        InputStream in = new FileInputStream(path);
        print(in);
    }
    
    /**
     * 获取WebRoot目录目录下db.properties文件
     * @throws IOException
     */
    public void read4() throws IOException{
        // ServletContext读取文件
        InputStream in = getServletContext().getResourceAsStream("/db.properties");
        // 打印方式
        print(in);
    }
    
    /**
     * 获取包目录下db.properties文件
     * @throws IOException
     */
    public void read3() throws IOException{
        // ServletContext读取文件
        InputStream in = getServletContext().getResourceAsStream("/WEB-INF/classes/cn/itcast/context/db.properties");
        // 打印方式
        print(in);
    }
    
    /**
     * 获取src目录下db.properties文件
     * @throws IOException   编译后,src文件不见了,全都放在class文件里
     */
    public void read2() throws IOException{
        // ServletContext读取文件
        InputStream in = getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        // 打印方式
        print(in);
    }
    
    /**
     * 传统方式读取资源文件
     *     交给服务器处理,相对的位置tomcat/bin
     * @throws IOException 
     */
    public void read1() throws IOException{
        // 获取输入流
        InputStream in = new FileInputStream("src/db.properties");
        print(in);
    }
    
    /**
     * 在控制台打印内容
     * @param in
     * @throws IOException
     */
    public void print(InputStream in) throws IOException{
        Properties pro = new Properties();
        // 加载
        pro.load(in);
        // 获取文件中的内容
        String username = pro.getProperty("username");
        String password = pro.getProperty("password");
        String desc = pro.getProperty("desc");
        
        System.out.println("用户名:"+username);
        System.out.println("密码:"+password);
        System.out.println("描述:"+desc);
    }
    
}

1.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    
    <h3>1.html的相对路径</h3>
    <a href="./demo5">demo5</a>
    <a href="demo5">demo5</a>
    
    <h3>1.html的绝对路径</h3>
    <a href="http://localhost/day09/demo5">demo5</a>
    <a href="/day09/demo5">demo5</a>
    
</body>
</html>

 

Servlet配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    
  <servlet>
    <servlet-name>ReadServlet</servlet-name>
    <servlet-class>cn.itcast.context.ReadServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ReadServlet</servlet-name>
    <url-pattern>/read</url-pattern>
  </servlet-mapping>
        
</web-app>

Servlet和location、302一起完成重定向

package cn.itcast.http;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 页面定时跳转
 * @author Administrator
 *
 */
public class RefreshServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().write("访问到了...");
        // 页面5秒会跳转到ServletDmo1
        response.setHeader("refresh", "5;url=/day09/1.html");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

 

package cn.itcast.http;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 和location和302一起完成重定向
 * @author Administrator
 *
 */
public class ServletDmo1 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 向页面输出内容
        response.setContentType("text/html;charset=UTF-8");
        // response.getWriter().write("向班长借钱...");
        // 我没钱
        response.setStatus(302);
        // 告诉我富班长的地址(重定向都是客户端的,带项目名)
        response.setHeader("location", "/day09/1.html");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    

  <servlet>
    <servlet-name>ServletDmo1</servlet-name>
    <servlet-class>cn.itcast.http.ServletDmo1</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>RefreshServlet</servlet-name>
    <servlet-class>cn.itcast.http.RefreshServlet</servlet-class>
  </servlet>

   <servlet-mapping>
    <servlet-name>ServletDmo1</servlet-name>
    <url-pattern>/servlet1</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>RefreshServlet</servlet-name>
    <url-pattern>/refresh</url-pattern>
  </servlet-mapping>
      
</web-app>

 

ServletContext对象读取资源文件以及重定向

标签:输出流   磁盘   iter   org   inf   read   path   cep   控制台   

原文地址:https://www.cnblogs.com/zsj03180204/p/11025252.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!