Servlet02

ServletContext

@WebServlet("/Test01")
public class Test01 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        //向ServletContext对象的map中添加数据
        context.setAttribute("name", "AAAAAAAA");
    }
}

@WebServlet("/Test02")
public class Test02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        //从ServletContext对象的map中获取数据
        String name = (String) context.getAttribute("name");
        System.out.println(name);
    }
}

@WebServlet("/Test03")
public class Test03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        //根据name从ServletContext对象的map中移除数据
        context.removeAttribute("name");
    }
}

访问顺序:Test01 -> Test02 -> Test03 -> Test02
控制台显示结果:
在这里插入图片描述

从web.xml中获取全局配置信息

web.xml:
<context-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
</context-param>

@WebServlet("/Test04")
public class Test04 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String name = (String) context.getInitParameter("encoding");
        System.out.println(name);
    }
}

获取资源路径

@WebServlet("/Test05")
public class Test05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       /* String getRealPath(String path);
       根据资源名称得到资源的绝对路径
       可以得到当前应用任何位置的任何资源*/

        Properties pro = new Properties();
        //web项目,查找文件时,要从类路径找
        String path = this.getServletContext().getRealPath("WEB-INF/classes/com/sjw/web/day20/new.Properties");
        //System.out.println(path);
        //C:\Users\Admin\IdeaProjects\JavaWeb\out\artifacts\JavaWeb_war_exploded\WEB-INF\classes\com\sjw\web\day20\new.Properties
        //关联属性文件路径
        pro.load(new FileInputStream(path));
        System.out.println(pro.getProperty("username"));
        //相应客户端
        resp.getWriter().write(path);
        resp.getWriter().write("====");
        resp.getWriter().write(pro.getProperty("username"));
    }
}

Servlet的转发

@WebServlet("/Test06")
public class Test06 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //转发请求
        RequestDispatcher rd = req.getRequestDispatcher("/Test07");
        rd.forward(req, resp);
    }
}

@WebServlet("/Test07")
public class Test07 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //响应
        resp.getWriter().write("data from Test07");
    }
}

在这里插入图片描述

Http常见的请求头

  1. ccept:浏览器可接受的MIME类型。
  2. Accept-Charset:浏览器可接受的字符集。
  3. Accept-Encoding:浏览器能够进行解码的数据编码方式,比如gzip。Servlet能够向支持gzip的浏览器返回经gzip编码的HTML页面。许多情形下这可以减少5到10倍的下载时间。
  4. Accept-Language:浏览器所希望的语言种类,当服务器能够提供一种以上的语言版本时要用到。
  5. Authorization:授权信息,通常出现在对服务器发送的WWW-Authenticate头的应答中。
  6. Connection:表示是否需要持久连接。如果Servlet看到这里的值为“Keep-Alive”,或者看到请求使用的是HTTP 1.1(HTTP 1.1默认进行持久连接),它就可以利用持久连接的优点,当页面包含多个元素时(例如Applet,图片),显著地减少下载所需要的时间。要实现这一点,Servlet需要在应答中发送一个Content-Length头,最简单的实现方法是:先把内容写入ByteArrayOutputStream,然后在正式写出内容之前计算它的大小。
  7. Content-Length:表示请求消息正文的长度。
  8. Cookie:这是最重要的请求头信息之一
  9. From:请求发送者的email地址,由一些特殊的Web客户程序使用,浏览器不会用到它。
  10. Host:初始URL中的主机和端口。
  11. If-Modified-Since:只有当所请求的内容在指定的日期之后又经过修改才返回它,否则返回304“Not Modified”应答。
  12. Pragma:指定“no-cache”值表示服务器必须返回一个刷新后的文档,即使它是代理服务器而且已经有了页面的本地拷贝。
  13. Referer:包含一个URL,用户从该URL代表的页面出发访问当前请求的页面。
  14. User-Agent:浏览器类型,如果Servlet返回的内容与浏览器类型有关则该值非常有用。

Http常见的响应头

  1. Allow: 服务器支持哪些请求方法(如GET、POST等)。
  2. Content-Encoding:文档的编码(Encode)方法。只有在解码之后才可以得到Content-Type头指定的内容类型。利用gzip压缩文档能够显著地减少HTML文档的下载时间。Java的GZIPOutputStream可以很方便地进行gzip压缩,但只有Unix上的Netscape和Windows上的IE 4、IE 5才支持它。因此,Servlet应该通过查看Accept-Encoding头(即request.getHeader(“Accept-Encoding”))检查浏览器是否支持gzip,为支持gzip的浏览器返回经gzip压缩的HTML页面,为其他浏览器返回普通页面。
  3. Content-Length:表示内容长度。只有当浏览器使用持久HTTP连接时才需要这个数据。如果你想要利用持久连接的优势,可以把输出文档写入
    ByteArrayOutputStream,完成后查看其大小,然后把该值放入Content-Length头,最后通过byteArrayStream.writeTo(response.getOutputStream()发送内容。
    Content-Type 表示后面的文档属于什么MIME类型。Servlet默认为text/plain,但通常需要显式地指定为text/html。由于经常要设置Content-Type,因此HttpServletResponse提供了一个专用的方法setContentType。
  4. Date 当前的GMT时间。你可以用setDateHeader来设置这个头以避免转换时间格式的麻烦。
  5. Expires 应该在什么时候认为文档已经过期,从而不再缓存它。
  6. Last-Modified 文档的最后改动时间。客户可以通过If-Modified-Since请求头提供一个日期,该请求将被视为一个条件GET,只有改动时间迟于指定时间的文档才会返回,否则返回一个304(Not Modified)状态。Last-Modified也可用setDateHeader方法来设置。
  7. Location 表示客户应当到哪里去提取文档。Location通常不是直接设置的,而是通过HttpServletResponse的sendRedirect方法,该方法同时设置状态代码为302。
  8. Refresh 表示浏览器应该在多少时间之后刷新文档,以秒计。除了刷新当前文档之外,你还可以通过setHeader(“Refresh”,“5; URL=http://host/path”)让浏览器读取指定的页面。
    注意:Refresh头不属于HTTP 1.1正式规范的一部分,而是一个扩展,但Netscape和IE都支持它。
  9. Server服务器名字。Servlet一般不设置这个值,而是由Web服务器自己设置。
  10. Set-Cookie 设置和页面关联的Cookie。Servlet不应使用response.setHeader(“Set-Cookie”, …),而是应使用HttpServletResponse提供的专用方法addCookie。参见下文有关Cookie设置的讨论。
  11. WWW-Authenticate客户应该在Authorization头中提供什么类型的授权信息?在包含401(Unauthorized)状态行的应答中这个头是必需的。例如,response.setHeader(“WWW-Authenticate”,“BASIC realm=\“executives\””)。
    注意:Servlet一般不进行这方面的处理,而是让Web服务器的专门机制来控制受密码保护页面的访问(例如.htaccess)。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值