JavaWeb知识小汇(1)——ServletContext

ServletContext

web容器启动时,为每个web应用程序都创建一个ServletContext对象,它代表了当前web应用

共享数据

​ 在此servlet保存的数据可以在另一个servlet中应用

@WebServlet("/context")
public class Context extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        String username = "www";
        servletContext.setAttribute("un", username);//设置数据
    }
}
@WebServlet("/contextGet")
public class contextGet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        String un = (String) servletContext.getAttribute("un");//获取数据
        resp.getWriter().println(un);
    }
}
获取初始化参数
<!--web.xml配置-->
<context-param>
    <param-name>url</param-name>
    <param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
@WebServlet("/contextGet")
public class contextGet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        //String un = (String) servletContext.getAttribute("un");
        //resp.getWriter().println(un);
        String url = servletContext.getInitParameter("url");
        resp.getWriter().println("url" + url);
    }
}
请求转发
@WebServlet("/context")
public class Context extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //ServletContext servletContext = this.getServletContext();
        //String username = "www";
        servletContext.setAttribute("un", username);
        servletContext.getRequestDispatcher("/contextGet").forward(req, resp);
    }
}
读取资源文件 Properties

防止资源读取失败pom.xml中的配置

<build>
    .......
      <resources>
        <resource>
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>**/*.properties</exclude>
                <exclude>**/*.xml</exclude>
             </excludes>
            <filtering>false</filtering>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
    ......
</build> 

Properties

1.在Java目录下新建properties文件(需要添加以上配置文件才能成功打包)

2.在resources目录下新建properties文件

都被打包在同一路径下classes,就是我们俗称的classpath

需要一个流

@WebServlet("/prop")
public class properServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext = this.getServletContext();
        InputStream is = servletContext.getResourceAsStream("/WEB-INF/classes/com/wjq/servlet/ja.properties");
        Properties properties = new Properties();
        properties.load(is);
        String un = properties.getProperty("username");
        String pwd = properties.getProperty("password");
        resp.getWriter().println(un);
        resp.getWriter().println(pwd);
    }
}

如有不对的地方欢迎指出,大家共同进步!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值