域对象 请求转发 请求重定向与HttpServletResponse响应

域对象获取全局配置信息与服务器上文件真实路径

(1.域对象(在一定范围内 存储信息的对象)
ServletContext 范围:整个程序中都可以访问到 并且只有一个(单例对象)
每个servlet都可以访问到这个域对象

获取ServletContext对象的方式
方式1:从ServletConfig对象中获取
方式2:从父类中直接获取
注意:所有的域对象都有 设置 获取 删除的方法
    // 方式1
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
        // 从servletConfig对象中获取
        ServletContext application = this.getServletConfig().getServletContext();
        // 添加一个数据到context域中
        // 相当于添加一个键值对
        application.setAttribute("username,"zhangsan");
    }
    // 方式2
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
        // 从父类中直接获取域对象
        Object object = this.getservletContext().getAttribute("username");
        System.out.println(object);     
(2.设置全局配置信息
    在web.xml中进行全局信息的配置 
    在<servlet></servlet>外添加全局信息
    <context-param>
        <param-name>key</param-name>
        <param-value>value</param-value>
    </context-param>
        // 获取全局配置信息
        public void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
             // 获取context
             ServletContext application = this.getServletContext();
             // 获取全局配置信息
             String value = application.getInitParameter("key");
             System.out.println(value);
         }
(3.获取服务器上的真实路径 并读取
使用Conttext域对象获取 可以获取到服务器上任意资源路径
// 在工程src文件夹下创建a.properties文件 并获取
    private void fun1() throws IOException, FileNotFoundException {
        // 获取a文件
        ServletContext application = this.getservletContext();
        // 获取服务器上的真实路径(绝对路径或磁盘路径)
        String path = application.getRealPath("/WEB/INF?classes/a.properties");
        // 读取文件
        Properties properties = new Properties();
        properties.load(new FileInputStream(path));
        System.out.println(properties.getProperty("key"));
    }

请求转发与请求重定向

(4.请求转发与请求重定向
// 请求转发:浏览器只是发起了一次请求 servlet内部做的请求转发 浏览器并不知道
public class Test01 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    System.out.println("要借钱");
    System.out.println("没有 找test02")
    // 获取域对象
    ServletContext application = this.getServletContext();
    // 从context域中 获取请求转发器
    RequestDispatcher dispatcher = application.getRuquestDispatcher();
    // 进行请求转发
    dispatcher.forward(request,response);
    System.out.println("钱已搞定");

 public class Test02 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    System.out.println("有钱 这是test02");
执行结果:
    要借钱
    没有 找test02
    有钱 这是test02
    钱已搞定

// 请求重定向:发起了两次请求(请求地址发生了变化)
   浏览器发起请求(请求Servlet)
   Servlet给浏览器一个响应
   在响应中会携带一个重定向响应头(头中有重定向的访问地址)
   浏览器接到这个响应后 发现重定向响应头 
   再一次发起请求 去访问重定向响应头中的地址
   // 通过添加重定向响应头的方式 请求重定向
   public class Test03 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    System.out.println("要借钱");
    System.out.println("没有 去找test04");
    // 添加重定向响应头
    // 注意:添加 头信息 访问地址的时候 需要写明工程名
    rewponse.setHeader("location","/sh-web-servlet/test04");
    // 添加重定向状态码
    response.setStatus(302);
    System.out.println("借钱去了");
}
public class Test04 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    System.out.println(钱被借走);
执行结果:
        要借钱
        没有 去找test04
        借钱去了
        钱被借走

服务器响应对象

(5.HTTPServletResponse 服务器响应对象
响应对象中有:
响应行 http/1.1 状态码200
响应头 告诉浏览器要做什么 例如响应给你的文件需要下载 以什么编码格式解析数据
响应体 响应回浏览器的数据
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        // 设置服务器的编码格式 tomcat默认的编码格式 iso-8859-1
        // response.setCharacterEncoding("UTF-8");
        // 告诉浏览器要使用什么编码格式来查看 添加响应头
        // response.setHeader("Content-type","text/html;charset=UTF-8");

        // 代替以上两句的方法
        response.setContextType("text/html;charset=UTF-8");
        // 给浏览器响应一句话
        // 从响应对象 HttpServletResponse 中获取对象
        // 注意:这个流对象不是自己创建 要从响应中获取
        PrintWriter out = response.getWrite();
        out.write("很开心");
    }
    // 刷新响应
    private void fun1(HttpServletResponse response) throws IOException {
        // 添加刷新头(每秒刷新一次)
        response.setIntHeader("refresh",1);
        // 添加随机数
        response.getWriter().write(Math.random() + "");
    }
    // 跳转响应
    private void fun3(HttpServletResponse response) throws IOException {
        // 设置响应的字符集
        response.seetContentType("text/html;charset=UTF-8");
        // 3秒后 跳转一个请求地址
        response.setHeader("refresh","3;url=http://www.baidu.com");
        response.getWriter().write("3秒后 跳转 很开心");

服务器上下载文件

(6.下载文件
    用户发送请求 请求到访问Servlet
    Servlet处理请求(把服务器上的图片 以流的形式 使用response 响应给用户浏览器)
    首先 把名字为 团子大家族.png的图片放到工程src文件夹下
        public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
            // 获取服务器上的图片路径
    String path = this.getServletContext().getRealPath("/WEB-INF/classes/团子大家族.png");
            // 字符串切割 获取图片的名字
            int index = path.lastIndexof("/");
            String filename = path.substring(index + 1);
            // 修改文件名字的字符集
            filename = new String(filename.getBytes(),"iso-8859-1");
            // 添加响应头(需要拼接文件的名字)
            response.setHeader("content-disposition","attachment;filename=" + filename);
            // 告诉浏览器文件下载的格式 添加响应头
            response.setHeader("content-type","image/png");
            // 从服务器中读取图片 边读边写
            FineInputStream fis = new FileInputstream(path);
            // 注意: 需要获取response当中 字节流
            ServletOutputstream sos = response.getOutputStream();
            int len = 0;
            byte[] b = new byte[1024];
        while ((len = fis.read(b)) != -1) {
            // 响应回浏览器
             // 如果只是单纯的把图片响应回去
             // 浏览器并不知道你要干什么(下载或浏览)
             // 需要通过响应头 通知浏览器  该文件是给你下载用的
             sos.write(b, 0, len);
        }
        // 注意:自己创建的流 自己关
         fis.close();
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值