HttpServletRequest接口


  1.1. ServletContext是Servlet上下文对象,该接口中也有这样的方法
      -void  setAttribute(String name,Object o)//向ServletContext中添加数据
      -Object getAttribute(String name);//从servletContext范围中读取数据
      -void removeAttribute(String name)//移除ServletContext范围中的数据
      ServletContext是怎样的一个范围?
      所有用户共享的一个范围对象,我们一般把servletContext变量命名为:application
      可见这个对象代表一个应用,一个webapp只有这样的对象,范围及大;
   1.2 . HttpServletRequest接口中常用的方法
      -String getParameter(String name)
      -Map getParameterMap();
      -Enumeration getParameterNames();
      -String[] getParameterValues(String name)
      
      -Sting getContextPath()//获取webapp的根路径
      -String getMethod()//获取浏览器请求方式
      -String getRequestURI()//获取请求的URI
      -StringBuffer getRequestURL()//获取请求的URL
      -Stirng getRemoteAddr() //获取客户端ip地址
      -String getServletPath()//获取servletpath
      
      -void  setAttribute(String name,Object o)//向request范围中添加数据
      -Object getAttribute(String name);//从request范围中获取数据
      -void removeAttribute(String name)//移除request范围中的数据

      
       -RequestDispatcher getRequestDispatcher(String path)//获取请求转发器,让转发器对象指向某个资源
     重点:  转发代码:request.getRequestDispatcher("/b").forward(request, response);   
     
      -void setCharacterEncoding(String env)
      
     
      
      -Cookie[]getCookies()讲Cookiie的时候讲
      -HttpSession getSession()讲session的时候讲
     1.3. httpServletRequest是一个怎样的范围?
         HttpServletRequest类型的变量通常命名为:request, 代表当前的本次请求,
              一次请求对应一个request对象,100个请求对应100个请求对象;

              请求范围很小,request只能完成同一次请求中传递数据。

案例如下:

   AServlet

 public class AServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //创建user对象
        User user=new User();
        user.setUsercode("111");
        user.setUsername("zhangsan");
        //将User对象存储到request对象中
        request.setAttribute("userObj", user);
        //从request范围中取出数据
//        Object obj=request.getAttribute("userObj");
//        response.getWriter().print(obj);
        //跳转
        //执行完AServlet之后,跳转到BServlet执行,将AServlet和BServlet放到同一次请求当中
        //必须使用转发技术
        //forward[转发]
        //获取请求转发器
//        RequestDispatcher dispatcher=request.getRequestDispatcher("/b");
//        //调用请求转发器的forward方法即可完成转发
//        dispatcher.forward(request, response);
        //转发[转发是一次请求]
        request.getRequestDispatcher("/b").forward(request, response);
    }

}

BServlet

public class BServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
         Object obj=request.getAttribute("userObj");
         response.getWriter().print(obj);

    }

}

             
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值