栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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文件中添加初始化参数

  
    url
    www.baidu.com
  

使用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();
    
    context.getRequestDispatcher("/getp").forward(req,resp);
}

4.读取资源文件

Properties:

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

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

 
        
            
                src/main/resources
                
                    ****.xml
                
                true
            
            
                src/main/java
                
                    ****.xml
                
                true
            
        

对应程序读取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);
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/878290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号