【Web入门系列】初探ServletContext

引入

ServletContext对象叫做Servlet的上下文对象,表示一个当前的web应用环境,一个web应用中只有一 个ServletContext对象。

ServletContext对象创建时机

创建时机:加载web应用时创建ServletContext对象。

得到对象:从ServletConfig对象的getServletContext方法得到。

获取web应用初始化参数

<?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">

    <context-param>
        <param-name>aaa</param-name>
        <param-value>value of aaa</param-value>
    </context-param>
    <context-param>
        <param-name>bbb</param-name>
        <param-value>value of bbb</param-value>
    </context-param>
    <context-param>
        <param-name>ccc</param-name>
        <param-value>value of ccc</param-value>
    </context-param>


    <servlet>
        <servlet-name>ContextParamServlet</servlet-name>
        <servlet-class>ysdrzp.context.ContextParamServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ContextParamServlet</servlet-name>
        <url-pattern>/contextParamServlet</url-pattern>
    </servlet-mapping>

</web-app>

 

public class ContextParamServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //得到ServletContext对象
        ServletContext context = this.getServletContext();
        System.out.println("aaa:"+context.getInitParameter("aaa"));
        Enumeration<String> enums = context.getInitParameterNames();
        while(enums.hasMoreElements()){
            String paramName = enums.nextElement();
            String paramValue  =context.getInitParameter(paramName);
            System.out.println(paramName+"="+paramValue);
        }
    }
}

注意:web应用参数可以在当前web应用的所有Servlet中获取。

ServletContext域对象

域对象的作用是用于保存数据,获取数据,可以在不同的动态资源之间共享数据。

/**
 * 保存数据
 */
public class SaveDataServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 1、获取域对象
        ServletContext context = this.getServletContext();

        // 2、保存数据
        context.setAttribute("name","haha");
        HuMan huMan = new HuMan("花花",18);
        context.setAttribute("human",huMan);
        System.out.println("保存数据成功");
    }
}

class HuMan{

    String name;

    int age;

    public HuMan(String name,int age){
        this.name =name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "HuMan{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
/**
 * 获取数据
 */
public class GetDataServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 1、获取域对象
        ServletContext context = this.getServletContext();

        // 2、保存数据
        String name = (String) context.getAttribute("name");
        System.out.println(name);
        HuMan huMan = (HuMan) context.getAttribute("human");
        System.out.println(huMan.toString());

        // 3、删除数据
        context.removeAttribute("name");
    }
}

转发技术

1、转发

a、地址栏不会改变  b、转发只能转发到当前web应用内的资源 c、可以在转发过程中,把数据保存到request域对象中

2、重定向

a、地址栏会改变,变成重定向到地址 b、重定向可以跳转到当前web应用,或其他web应用,甚至是外部域名网站。c、不能再重定向的过程中把数据保存到request域对象中。

结论:如果要使用request域对象进行数据共享,只能用转发技术。

/**
 * 转发(效果:跳转页面)
 */
public class ForwardServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //保存数据到request域对象
        request.setAttribute("name", "rose");

        // 转发
        /*RequestDispatcher rd = this.getServletContext().getRequestDispatcher("/getDataServlet");
        rd.forward(request, response);*/
        this.getServletContext().getRequestDispatcher("/getDataServlet").forward(request, response);
    }
}

 

转载于:https://www.cnblogs.com/ysdrzp/p/9873076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值