# JavaWeb笔记(五)---- ServletContext

# JavaWeb笔记(五)---- ServletContext,第1张

本人个人学习笔记 未经授权不允许转发

JavaWeb笔记(五)---- ServletContext 目录

1. 共享数据
2. 获取初始化参数
3. 请求转发
4. 读取资源文件

1. 共享数据

Web容器在启动时会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用。他可以起到共享数据的作用。在一个servlet中保存的数据可以被另一个servlet拿到

java目录下创建两个类一个叫 GetServletContext 另一个叫 SetServletContext

GetServletContext:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //设置显示的页面类型和字符集utf-8
    resp.setContentType("text/html;charset=utf-8");
    ServletContext context = this.getServletContext();
    String username = (String) context.getAttribute("username");
    resp.getWriter().print("名字:"+username); 
}

SetServletContext:

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	ServletContext context = this.getServletContext();
    String username = "张三";
    context.setAttribute("username",username); //将一个数据保存在了ServletContext中
}

2.获取初始化参数

在xml文件中添加初始化参数

  <context-param>
    <param-name>urlparam-name>
    <param-value>www.baidu.comparam-value>
  context-param>

使用getInitParameter获取初始化参数

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html;charset=utf-8");
    ServletContext context = this.getServletContext();
    String text = (String) context.getInitParameter("url");
    resp.getWriter().print(text);
}

3.请求转发

网页显示路径不会变化 但是内容会改变

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    ServletContext context = this.getServletContext();
    /*与下面等同
    RequestDispatcher dispatcher = context.getRequestDispatcher("/getp");
    dispatcher.forward(req,resp);
    */
    context.getRequestDispatcher("/getp").forward(req,resp);
}

4.读取资源文件

Properties:

  • 在java目录下创建aa.properties
  • 在resource目录下创建db.properties
    内容:
username=Admin
password=123456

两个properties都被打包到了 classes (类路径下)

 
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/**.propertiesinclude>
                    <include>**/**.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/**.propertiesinclude>
                    <include>**/**.xmlinclude>
                includes>
                <filtering>truefiltering>
            resource>
        resources>

对应程序读取properties

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        ServletContext context = this.getServletContext();
        InputStream is = context.getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties prop = new Properties();
        prop.load(is);
        String username = (String) prop.getProperty("username");
        String pwd = (String) prop.getProperty("password");
        resp.getWriter().print(username+":"+pwd);
    }

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/919718.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存