JavaWeb Knowledge Collection (1)-ServletContext

ServletContext

When the web container is started, a ServletContext object is created for each web application, which represents the current web application

Share data

​ The data saved in this servlet can be used in another 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);
    }
}
Get initialization parameters
<!--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);
    }
}
Request forwarding
@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);
    }
}
Read resource file Properties

Prevent resource read failure. Configuration in 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. Create a new properties file in the Java directory (you need to add the above configuration file to successfully package)

2. Create a new properties file in the resources directory

All are packaged in the same path as classes, which is what we commonly call classpath

Need a stream

@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);
    }
}

If there is something wrong, please point it out and make progress together!

Guess you like

Origin blog.csdn.net/weixin_45734378/article/details/112977351