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);
    }
}
上一篇:从头看看Tomcat启动Spring容器的原理


下一篇:servlet简介