首页 > 其他分享 >Servlet20 - 设置初始化参数

Servlet20 - 设置初始化参数

时间:2023-01-23 10:44:10浏览次数:37  
标签:初始化 void Demo01Servlet Servlet20 init 参数 config public

Servlet 设置初始化参数

Servlet 的初始化,两个 init 方法,如果想在初始化时做一些准备工作,可以重写 init 方法
  • 无参 init()

    • public void init(){
      }
      
  • 带参 init( ServletConfig config )

    • public void init( ServletConfig config){
      	this.config = config;
          init();
      }
      

配置文件 web.xml 中设置初始化参数

在 web.xml 配置文件中进行 servlet 配置时,添加 init-param 设置键值对类型的初始化参数,通过 param-name 可以获取 param-value

  • 设置参数

    • <servlet>
          <servlet-name>Demo01Servlet</servlet-name>
          <servlet-class>com.atguigu.servlet.Demo01Servlet</servlet-class>
          <init-param>
              <param-name>hello</param-name>
              <param-value>world</param-value>
          </init-param>
      </servlet>
      
      <servlet-mapping>
          <servlet-name>Demo01Servlet</servlet-name>
          <url-pattern>/demo01</url-pattern>
      </servlet-mapping>
      
  • 在 init() 方法初始化时,从配置文件中获取参数,getServletConfig() 得到 ServletConfig 对象,再调用方法

    • public class Demo01Servlet extends HttpServlet {
          @Override
          public void init() throws ServletException {
              ServletConfig config = getServletConfig();
              String initValue = config.getInitParameter("hello");
              System.out.println("initValue = " + initValue);
          }
      }
      

使用注解的方式设置初始化参数

在 @WebServlet 的注解中不仅可以设置服务器映射,也可以设置初始化参数值

  • 在 @WebServlet 中设置 url 和 initParam

    • @WebServlet(urlPatterns = {"/demo02"} ,
          initParams = {
              @WebInitParam(name = "hello", value = "world"),
              @WebInitParam(name = "uname", value = "ashen")
          }
      )
      public class Demo01Servlet extends HttpServlet {
          @Override
          public void init() throws ServletException {
              ServletConfig config = getServletConfig();
              String initValue = config.getInitParameter("uname");
              System.out.println("initValue = " + initValue);
          }
      }
      

配置文件 web.xml 中设置获取上下文 context

在配置文件中配置所有 Servlet 公用的 context-param

  • <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
  • 在初始化 init 方法中获取上下文参数的值

  • getServletContext() 方法得到 ServletContext 对象,通过该对象调用 .getInitParameter() 方法传入 name 获取 value

    • public class Demo01Servlet extends HttpServlet {
          @Override
          public void init() throws ServletException {
              ServletContext servletContext = getServletContext();
              String context = servletContext.getInitParameter("contextConfigLocation");
              System.out.println("context :" +context);
          }
      }
      
  • 上下文参数 context-param 除了在初始化中获取,也可以在 service 服务过程中获取

  • 先通过 request 对象获取 session 再通过 session 调用 getServletContext() 方法

    • @Override
      protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          ServletContext servletContext = req.getSession().getServletContext();
          servletContext.getInitParameter("contextConfigLocation");
      }
      

标签:初始化,void,Demo01Servlet,Servlet20,init,参数,config,public
From: https://www.cnblogs.com/Ashen-/p/17065038.html

相关文章