Servlet-11.20

共享数据:

      域对象:一个有作用的范围的对象,可以在范围内共享数据 request域对象:代表一次请求的范围,一般用于请求转发的多个资源中共享数据 方法:

1.void setAttribute(String name,Object obj):存储数据

2.Object getAttribute(String name):通过key值获取数据

3.void removeAttribute(String name):通过key值移除数据 请求转发:一种在服务器内部的资源跳转方式

步骤:

1.通过request对象获取转发对象 RequestDispatcher getRequestDistpatcher(String path)

2.使用requestDispatcher 对象进行转发:forward(Request,Response)

@WebServlet("/requestDemo7")
public class RequestDemo7 extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletRespon
se response) throws ServletException, IOException {
 System.out.println("Demo7....");
 //将处理事情发送Demo8内容中去
 //将数据存储request域中
 request.setAttribute("msg","hello"); // msg = hello
 //转发设置
 RequestDispatcher requestDispatcher = request.getRequestDispatc
her("/requestDemo8");//在服务器内部进行处理过程
 //将当前此次的请求和响应对象一同发送过去。
 requestDispatcher.forward(request,response);
// System.out.println("Demo7已经完成转发任务....");
 }
 protected void doGet(HttpServletRequest request, HttpServletRespons
e response) throws ServletException, IOException {
           this.doPost(request,response);
 }
}
@WebServlet("/requestDemo8")
public class RequestDemo8 extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletRespon
se response) throws ServletException, IOException {
 System.out.println("Demo8....");
 //获取request域中的值
 Object obj = request.getAttribute("msg");
 System.out.println(obj);
 }
 protected void doGet(HttpServletRequest request, HttpServletRespons
e response) throws ServletException, IOException {
 this.doPost(request,response);
 }
}

获取ServletContext对象-- 是一个全局的存储信息的域对象,从服务器开始就创建存在的,当服务器关 闭时,对象销毁。

@WebServlet("/requestDemo9")
public class RequestDemo9 extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletRespon
se response) throws ServletException, IOException {
 ServletContext context = request.getServletContext();
 System.out.println(context);
 context.setAttribute("name","张三");//将值存放到ServletContext
 }
 protected void doGet(HttpServletRequest request, HttpServletRespons
e response) throws ServletException, IOException {
 this.doPost(request,response);
 }
}
@WebServlet("/requestDemo10")
public class RequestDemo10 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletRespon
se response) throws ServletException, IOException {
 ServletContext context = request.getServletContext();
 Object obj = context.getAttribute("name");
 System.out.println(obj);
 }
 protected void doGet(HttpServletRequest request, HttpServletRespons
e response) throws ServletException, IOException {
 this.doPost(request,response);
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭淞源

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值