Java中ServletContext,response对象

获取全局配置信息

    XML文件中配置全局配置信息

    <!-- 配置全局配置信息 -->
  <context-param>
    <param-name>key</param-name>
    <param-value>value</param-value>
  </context-param>
  <servlet>
    <servlet-name>demo01</servlet-name>
    <servlet-class>com.lanou3g.Demo01</servlet-class>
    <!-- 配置demo01中servlet的配置信息 -->
    <init-param>
        <param-name>username</param-name>
        <param-value>xiaoming</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo01</servlet-name>
    <url-pattern>/demo01</url-pattern>
  </servlet-mapping>
    Java代码中

    public class Demo01 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // fun1()
    // 获取context域对象
    ServletContext application = this.getServletContext();
    // 获取全局配置信息
    String value = application.getInitParameter("key");
    System.out.println(value);
    }

    private void fun1() {
        // 获取config对象
        ServletConfig config = this.getServletConfig();
        // 取出Servlet的配置信息
        String value = config.getInitParmeter("username");
        System.out.println(value);
    }

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

}

获取服务器上的真实路径并读取

/WEB-INF/classes/ 在这个路径下创建一个文件 a.properties
    在XML文件中配置Demo02

  <servlet>
    <servlet-name>demo02</servlet-name>
    <servlet-class>com.lanou3g.Demo02</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo02</servlet-name>
    <url-pattern>/demo02</url-pattern>
  </servlet-mapping>
public class Demo02 extends HttpServlet {

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

    private void fun1() throws IOException, FileNotFoundException {
        // 获取文件
        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"));
    }

    }

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

}

请求转发

    请求转发
    注意:浏览器只是发起了一次请求
    Servlet内部做的请求转发 浏览器 并不知道
    在XML文件中 配置Demo03 Demo04

    <servlet>
    <servlet-name>demo03</servlet-name>
    <servlet-class>com.lanou3g.Demo03</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo03</servlet-name>
    <url-pattern>/demo03</url-pattern>
  </servlet-mapping>

  <servlet>
    <servlet-name>demo04</servlet-name>
    <servlet-class>com.lanou3g.Demo04</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo04</servlet-name>
    <url-pattern>/demo04</url-pattern>
  </servlet-mapping>
    public class Demo03 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("我要借钱");
        System.out.println("我没钱, demo4可能有");
        // 获取Context域对象
        ServletContext application = this.getServletContext();
        // 从Context域中获取转发器
        RequestDispatcher requestDispatcher = application.getRequesDispatcher;
        // 进行请求转发
        requestDispatcher.forward(request,response);
        System.out.println("钱我搞定了");
    }

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

}

public class Demo04 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("我demo4 有钱");
    }

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

}

这里写图片描述

服务器的响应对象

    HttpServlerResponse 服务器的响应对象
    响应对象中又什么
    响应行
    响应头 告诉浏览器要做什么 例如: 响应给你的文件需要下载  以什么编码格式解析数据
    响应体 响应回浏览器的数据
    在XML文件中配置Demo05

    <servlet>
    <servlet-name>demo05</servlet-name>
    <servlet-class>com.lanou3g.Demo05</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo05</servlet-name>
    <url-pattern>/demo05</url-pattern>
  </servlet-mapping>
    public class Demo5 extends HttpServlet {

    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.setContentType("text/html;charset=UTF-8");

        // 给浏览器响应一句话
        // 从响应对象HttpServletResponse中获取流对象
        // 注意: 这个流对象不是你这句创建 要从响应中获取
        PrintWrite out = response.getWrite();
        out.write("你好");
    }

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

}

下载文件

    在XML中配置Demo06

    <servlet>
    <servlet-name>demo06</servlet-name>
    <servlet-class>com.lanou3g.Demo06</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo06</servlet-name>
    <url-pattern>/demo06</url-pattern>
  </servlet-mapping>
    public class Demo06 extends HttpServlet {

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

        // 获取服务器上的图片路径
        ServletContext application = this.getServletContext();
        String path = application.getRealPath("/WEB-INF/classes/团子大家族.png");
        // 字符串切割 获取图片名字
        int index = path.lastIndexOf("/");
        String file那么 = path.substring(index + 1);
        // 修改文件名字的字符集
        filename = new String(filename.getBytes(), "iso-8859-1");
        // 添加响应头(需要拼接文件的名字)
        response.setHeader("content-disposition","attachment;filename=" + filename);
        // 高速浏览器下载的格式 添加响应头

         // 响应回浏览器
         // 如果只是单纯的把图片响应回去
         // 浏览器并不知道你要干什么(下载或浏览)
         // 需要响应头 通知浏览器 这个文件是下载用的

        // 图片是什么格式就写什么格式
        // 图片是jpg格式 就写 image/jpeg
        response.setHeader("content-type","image/png");
        // 从服务器中读取图片
        FileInputStream 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);
        }
        // 注意: 自己创建的流 自己关
        // OutputStream是系统创建的 所以系统关
        fis.close();
    }

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

}

请求重定向

    请求重定向
    浏览器发起请求(请求Servlet)
    Servlet给浏览器一个响应
    在响应中会携带一个重定向响应头(头中有重定向的访问地址)
    再一次发起请求 去访问重定向头中的地址

    请求重定向和请求转发的区别
    请求重定向是发起了两次请求(请求地址发生了变化)
    请求转发只发起了一次请求

    响应时要注意的细节
        从response中获取的字符流和字节流 在同一个Servlet中 不能同时使用
    在XML文件中配置Demo07 Demo08

    <servlet>
    <servlet-name>demo07</servlet-name>
    <servlet-class>com.lanou3g.Demo07</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo07</servlet-name>
    <url-pattern>/demo07</url-pattern>
  </servlet-mapping>

    <servlet>
    <servlet-name>demo08</servlet-name>
    <servlet-class>com.lanou3g.Demo08</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo08</servlet-name>
    <url-pattern>/demo08</url-pattern>
  </servlet-mapping>
    public class Demo07 extends HttpServlet {

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

        System.out.println("我要借钱");
        System.out.println("我没钱,找Demo08");
        // 添加重定向响应头
        // 注意:添加头信息请求地址的时候 需要写明工程名
        response.setHeader("location", "/SH-web-servlet-02/demo08");
        //添加重定向状态码
        response.setStatus(302);
        System.out.println("我去了");
    }

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

}

public class Demo08 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("我借钱给你");

    }

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

}

这里写图片描述

刷新页面

    在XML文件中配置Demo09

    <servlet>
    <servlet-name>demo09</servlet-name>
    <servlet-class>com.lanou3g.Demo09</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo09</servlet-name>
    <url-pattern>/demo09</url-pattern>
  </servlet-mapping>
    public class Demo09 extends HttpServlet {

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

        // 添加刷新头(每秒刷新1次)
        response.setIntHeader("refresh", 1);
        // 添加随机数
        response.getWriter().write(Math.random() + "");
    }

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

}

跳转网页

    在XML文件中配置Demo010

    <servlet>
    <servlet-name>demo10</servlet-name>
    <servlet-class>com.lanou3g.Demo10</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>demo10</servlet-name>
    <url-pattern>/demo10</url-pattern>
  </servlet-mapping>
    public class Demo10 extends HttpServlet {

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

        // 设置响应的字符集
        response.setContentType("text/html;charset=UTF-8");
        // 3秒后 跳转一个请求地址
        response.setHeader("refresh", "3;url=/SH-web-servlet-02/demo08");
        response.getWriter().write("3秒后跳转");
    }

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

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值