Javaweb学习----ServletConfig和ServletContext

ServletConfig

	即Servlet的配置信息类

	是一个作用域,该作用域从Servlet服务器运行开始,到服务器停止结束

作用:

	1.可以获取Servlet程序的别名Servlet-name的值

	2.获取初始化参数init-param
   ServletConfig servletConfig = getServletConfig();
            System.out.println(servletConfig.getInitParameter("username"));
    
     <init-param>
                <param-name>username</param-name>
                <param-value>root</param-value>
    
            </init-param>
            <init-param>
                <param-name>password</param-name>
                <param-value>123456</param-value>
            </init-param>

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = getServletConfig().getServletContext();
        String username = servletContext.getInitParameter("username");
        System.out.println("用户名"+username+"密码"+servletContext.getInitParameter("password"));
        System.out.println("lujing"+servletContext.getRealPath("/"));
    }

3.获取Servletcontext对象

getServletContext()方法

Servlet程序和ServletConfig对象都是Tomcat创建的,Servlet程序默认是第一次访问时创建 每一个Servletconfig对应自己的Servlet程序

	在继承HTTPServlet类,重写init方法时,必须先调用父类的init方法

ServletContext类

	1.是一个接口,它表示Servlet上下文对象

	2.一个web工程,只有ServletContext对象实体

	3.是一个域对象

	什么是域对象:

	可以像map一样存储数据的对象,叫域对象,域指的是存取数据的操作范围

作用:

1.获取web.xml中配置上下文参数context-param

    public class HelloServlet4 extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext servletContext = getServletConfig().getServletContext();
            String username = servletContext.getInitParameter("username");
            System.out.println("用户名"+username+"密码"+servletContext.getInitParameter("password"));
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        }
    }
      <context-param>
            <param-name>username</param-name>
            <param-value>context</param-value>
        </context-param><context-param>
            <param-name>password</param-name>
            <param-value>context1</param-value>
        </context-param>

public class ContextServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        servletContext.setAttribute("aaa","123");
        System.out.println(servletContext.getAttribute("aaa"));
    }


}


2.获取当前的工程路径

3.获取工程部署后在服务器硬盘上的绝对路径

    System.out.println("用户名"+username+"密码"+servletContext.getInitParameter("password"));
    //读取当前web在文件目录中封装的绝对路径  /能访问到web目录 可以在后面加子目录
    System.out.println("lujing"+servletContext.getRealPath("/"));
    //读取当前web在文件目录中封装的名字
    System.out.println("lujing"+servletContext.getContextPath());

4.像map一样存取数据
ServletContext是在web工程启动时创建,在web工程停止的时候销毁 调用setAttribute方法,如果重新部署可以在整个项目工程内访问到数据,但是重启服务器就会销毁数据

    public class ContextServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            ServletContext servletContext = getServletContext();
            servletContext.setAttribute("aaa","123");
            System.out.println(servletContext.getAttribute("aaa"));
        }
    
    
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值