HttpServlet接口的使用

研究: javax.servlet.http.HttpServletRequest接口
      1、HtpServletRequest是一个接口,Servlet规范中重要接口之一
      2、继承关系:
         public interface HttpServletRequest extends ServletRequest{}
      3、HttpServletRequest接口的实现时WEB容器负责实现的,Tomcat服务器有自己的实现。但是程序员还是需要面向HttpServletRequest接口调用方法即可,不需要关心具体的实现了。
      4、HttpServletRequest这个对象中封装了那些信息呢?
         --请求方式
         --URI
         --协议版本号
         --表单提交的数据
         。。。。。。。
      5、HttpServletRequest一般变量的名字叫做:request,表示请求,HttpServletRequest对象代表一次请求,一次请求对应一个request对象。
      6、HttpServletRequest接口中常用的方法:
         // 用于表单
         --String getParamter(String name);  // 根据参数名称获取参数
         --Map getParameterMap();  
         --Enumeration getParameterNames();   
         --String[] getParameterValues(String name);  // 适合取复选框中的数据

         
         --void setAttribute(String name,Object o)   // 从request范围中添加数据
         --Object getAttribute(String name)   // 从request范围中读取数据
         --void removeAttribute(String name)   // 移除request范围中的数据

         --RequestDispathcer getRequestDispathcher(String path)  // 获取请求转发器,让转发器对象指向某个资源
         	//转发是一次请求
         	request.getRequestDispatcher("/b").forward(request,response);
         	



         --void setCharacterEncodeing(String env)

         --String getRemoteAddr()  // 获取客户端的地址
         --String getContextPath() // 获取上下文
         --String getMethod()  // 获取请求方法
         --String getRequestURI() // 获取请求的URI
         --StringBuffer getRequestURL() // 获取请求的URL
         --String getServletPath()  // 获取Servlet path (即我们在web.xml中配置完Servlet之后的路径)

         --Cookie[] getCookies() // 后面讲
         --HttpSession getSession()  // 后面讲Session



    关于范围对象的选择:
          ServletContext  应用范围,可以跨用户传递数据
          ServletRequest  请求范围,只能在同一个请求传递数据【可以跨Servlet传递数据,但是这多个Servlet必须在同一个请求当中】



ServletContext是一个怎样的范围?
    所有用户共享的一个范围对象,我们一般把ServletContext变量命名为:application
    可见这个对象代表一个应用。一个webapp只有一个这样的对象。范围极大
ServletContext是Servlet上下文对象,该接口中也有这样的几个方法:
    --void setAttribute(String name,Object o)   // 从ServletContext范围中添加数据
    --Object getAttribute(String name)   // 从ServletContext范围中读取数据
    --void removeAttribute(String name)   // 移除ServletContext范围中的数据

猜你喜欢

转载自blog.csdn.net/qq_36826506/article/details/83181493
今日推荐