目录
①获取web.xml中配置的上下文参数context-param
ServletConfig类从类名来看,就知道是Servlet程序的配置信息类
Servlet程序和ServletConfig对象都是由Tomcat负责创建,我们负责使用
Servlet程序默认是第一次访问的时候创建的,ServletConfig是每个Servlet程序创建时,就创建一个对应的ServletConfig对象
如果是重写了init()方法,需要自己写一个super.init(config)要不然会出现空指针异常
①可以获取Servlet程序的别名servlet-name的值
②获取初始化参数init-para
③获取ServletContext对象·
- @Override
- public void init(ServletConfig servletConfig) throws ServletException {
- System.out.println("2 初始化方法");
-
- // 可以获取Servlet程序的别名servlet-name的值
- System.out.println("别名:"+servletConfig.getServletName());
-
- // 获取初始化参数init-para
- System.out.println("init-para初始化参数username的值是:"+servletConfig.getInitParameter("username"));
- System.out.println("init-para初始化参数url的值是:"+servletConfig.getInitParameter("url"));
-
- // 获取ServletContext对象·
- System.out.println(servletConfig.getServletContext());
- }
- <servlet>
- <servlet-name>HelloServlet2servlet-name>
- <servlet-class>com.company.web_tomcat.HelloServlet2servlet-class>
-
-
- <init-param>
- <param-name>usernameparam-name>
- <param-value>rootparam-value>
- init-param>
-
- <init-param>
-
- <param-name>urlparam-name>
-
- <param-value>jdbc:mysql://localhost:3306/testparam-value>
- init-param>
-
- servlet>
-
- <servlet-mapping>
- <servlet-name>HelloServlet2servlet-name>
- <url-pattern>/hellourl-pattern>
- servlet-mapping>
是一个接口,表示Servlet上下文对象
一个web工程,只有一个ServletContext对象实例
ServletContext对象是一个域对象
ServletContext是在web工程部署启动的时候创建,在web工程停止的时候销毁
是可以像Map一样存取数据的对象,叫域对象
这里的域指的是存取数据的操作范围---整个web工程
- <servlet>
- <servlet-name>ContextServletservlet-name>
- <servlet-class>com.company.web_tomcat.ContextServletservlet-class>
- servlet>
- <servlet-mapping>
- <servlet-name>ContextServletservlet-name>
- <url-pattern>/ContextServleturl-pattern>
- servlet-mapping>
-
-
- <context-param>
- <param-name>usernameparam-name>
- <param-value>contextparam-value>
- context-param>
- <context-param>
- <param-name>passwordparam-name>
- <param-value>rootparam-value>
- context-param>
- public class ContextServlet extends HttpServlet {
-
-
- //④像Map一样存取数据
-
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // ①获取web.xml中配置的上下文参数context-param
- ServletContext context = getServletConfig().getServletContext();
-
- String username = context.getInitParameter("username");
- System.out.println("username:" + username);
- System.out.println("password:" + context.getInitParameter("password"));
- //②获取当前的工程路径,格式:/工程路径
- System.out.println("当前工程路径:" + context.getContextPath());
- //③获取工程部署后在服务器硬盘上的绝对路径
- // / 斜杠被服务器解析地址为:http://ip:port/工程名/ 映射到IDEA代码的web目录
- System.out.println("工程部署的绝对路径:" + context.getRealPath("/"));
-
-
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- }
- }
只要有ServletContext对象,并且保存了数据,不论在哪个地方,都可以取到(前提是在一个web工程中,且这个web工程没有被销毁)
- public class ContextServlet1 extends HttpServlet {
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- // ServletContext context = getServletConfig().getServletContext();
- // 用下面获取ServletContext对象这种方法更简洁
- ServletContext context = getServletContext();
-
- context.setAttribute("key1","value1");
- System.out.println("Context1中获取域数据key1的值是:"+context.getAttribute("key1"));
- System.out.println("Context1中获取域数据key1的值是:"+context.getAttribute("key1"));
- System.out.println("Context1中获取域数据key1的值是:"+context.getAttribute("key1"));
- }
-
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
-
- }
- }