ServletContext的应用

ServletContext的应用

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

  1. 共享数据

    我在这个servlet中保存的数据,可以在另外一个servlet中拿到

    package com.chen.servelt;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class Helloservlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
    
            //this.getServletContext();  Servelt上下文
            //this.getServletConfig();   Servlet配置
            //this.getInitParameter();   初始化参数
            ServletContext servletContext = this.getServletContext();
            String username = "chen";//数据
            servletContext.setAttribute("username",username);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            super.doPost(req, resp);
        }
    }
    
    
    package com.chen.servelt;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class GetServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("text/html");
            resp.setCharacterEncoding("UTF-8");
            ServletContext servletContext = this.getServletContext();
            String username = (String) servletContext.getAttribute("username");
            PrintWriter writer = resp.getWriter();
            writer.print("名字为"+username);
    
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
    
        <servlet>
            <servlet-name>hello</servlet-name>
            <servlet-class>com.chen.servelt.Helloservlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>hello</servlet-name>
            <url-pattern>/hello</url-pattern>
        </servlet-mapping>
    
        <servlet>
            <servlet-name>getc</servlet-name>
            <servlet-class>com.chen.servelt.GetServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>getc</servlet-name>
            <url-pattern>/getc</url-pattern>
        </servlet-mapping>
    </web-app>
    
    
  2. 获取初始化对象

    	<context-param>
            <param-name>url</param-name>
            <param-value>jdbc:mysql://localhost:3307/chen</param-value>
        </context-param>
     	
    	<servlet>
            <servlet-name>gp</servlet-name>
            <servlet-class>com.chen.servelt.ServletDemo03</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>gp</servlet-name>
            <url-pattern>/gp</url-pattern>
        </servlet-mapping>
    
    package com.chen.servelt;
    
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    public class ServletDemo03 extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            ServletContext servletContext = this.getServletContext();
            String url = servletContext.getInitParameter("url");
            resp.getWriter().print(url);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            doGet(req, resp);
        }
    }
    
    
    1. 请求转发

          @Override
              protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
                  resp.setContentType("text/html");
                  resp.setCharacterEncoding("UTF-8");
                  ServletContext servletContext = this.getServletContext();
                  String username = (String) servletContext.getAttribute("username");
                  PrintWriter writer = resp.getWriter();
                  writer.print("名字为"+username);
      
          <servlet>
              <servlet-name>sd4</servlet-name>
              <servlet-class>com.chen.servelt.ServletDemo04</servlet-class>
          </servlet>
          <servlet-mapping>
              <servlet-name>sd4</servlet-name>
              <url-pattern>/sd4</url-pattern>
          </servlet-mapping>
      

4.读取资源文件

username=root
password=123456
package com.chen.servelt;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ServletDemo05 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        InputStream resourceAsStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
        Properties properties = new Properties();
        properties.load(resourceAsStream);
        String user = properties.getProperty("username");
        String pwd = properties.getProperty("password");
        resp.getWriter().print(user+":"+pwd);
    }

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

	<servlet>
        <servlet-name>sd5</servlet-name>
        <servlet-class>com.chen.servelt.ServletDemo05</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>sd5</servlet-name>
        <url-pattern>/sd5</url-pattern>
    </servlet-mapping>
上一篇:过滤器


下一篇:Servlet(三)ServletContext