ServletContext


web容器在启动的时候,它会为每个web容器都创建一个ServletContext对象,它代表了当前的web应用,由this.ServletContext来管理

- 共享数据(会通过section、request替换)

? 通过这个Servlet保存在ServletContext中的数据,可以在另外一个Servlet中通过ServletContext拿到

关键词:Attribute、ContenType、CharactorEncoding

public class HelloServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        this.getInitParameter()  初始化参数
//        this.getServletConfig()  servlet配置
//        this.getServletContext() servlet上下文
        ServletContext context = this.getServletContext();
        String username = "YGW";
        context.setAttribute("username", username);  //属性名为username, 值为username中所带的值
//        System.out.println("hello");
    }

}


public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext getContext = this.getServletContext();
        String username = (String) getContext.getAttribute("username");

        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        resp.getWriter().print("名字:" + username);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
	
        Hello
        com.Gw.servlet.HelloServlet
    
    
        Hello
        /Hello
    
    
        getSer
        com.Gw.servlet.GetServlet
    
    
        getSer
        /getSer
    

- 获取初始化参数(几乎不用)

关键词:InitParameter、context-param、param-name、param-value

public class servletContext extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        String url = context.getInitParameter("url");
        resp.getWriter().print(url);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

    
        url
        jdbc:mysql://localhost:3306/mybatis
    
	    
        gp
        com.Gw.servlet.servletContext
    
    
        gp
        /gp
    

- 请求转发(request替换)

关键词:RequestDispatcher、forward

public class servletDispatcher extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext context = this.getServletContext();
        RequestDispatcher gp = context.getRequestDispatcher("/gp");  //注意使用转发时的格式
        gp.forward(req, resp);  //调用forward方法实现请求转发
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
    
        sd
        com.Gw.servlet.servletDispatcher
    
    
        sd
        /sd
    

- 读取资源文件(常用类加载、反射替换)

关键词:InputStream、getResourceAsStream、Properties

public class contextProperties extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream resourceAsStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        //注意提交时的相对位置以target中的位置为准,用绝对位置放在服务器中可能会发生变化
        Properties prop = new Properties();
        prop.load(resourceAsStream);
        String user = prop.getProperty("username");
        String password =  prop.getProperty("password");
        resp.getWriter().print(user + ":" + password);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
    
        cp
        com.Gw.servlet.contextProperties
    
    
        cp
        /cp
    
username = root
password = 123456

特别说明:

? 资源如果不能加载,可能将其放置在类路径上,需要在当前的pom.xml中加入build代码:


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