getServletContext和getServletConfig

getServletContext和getServletConfig在这个类 HelloServlet 中可以直接引用,是因为其父类HttpServlet的父类GenericServlet已经实现了该方法。所以这里并不是通过request得到这两个 方法,而是直接调用。GenericServlet方法中实现了一些基本的方法,比如 getServletInfo,getSerletName,getInitParameter方法等,都可以直接引用吗?还是。

          1:getServletContext()取得的是 <context-param>配置的参数
               getServletConfig()取得的是 <servlet> <init-param>配置的参数

           2: getServletContext()应用于整个web App,而getServletConfig()仅应用于当前Servlet。 但是ServletConfig对象拥有ServletContext的引用。所以可以通过getServletConfig()来获得 ServletContext,从而得到web App的初始值。

getServletContext和getServletConfig - 陈吉思汗 - 沉默NO金
      
       利用Servlet的getServletContext().getContext()方法,可以得到应用程序的上下文。 所以可以通过该方法和servlet容器进行通信,比如转发请求到另一个web应用的jsp上去,如getServletContext().getContext(). RequestDispatcher("/a.jsp")。注意这是需要在servlet容器中(tomcat的话为配置文件中的context元素节点的crossContxt属性)指定crossContxt属性为true。不过为了安全起见一般的不这样搞。
      也可以通过 getServletContext().setAttribute方法为不同的sevlet之间或者不同的客户端(ie浏览器)之间设置共享属性,但是只能在一个web应用中共享,不能在不同web应用中共享。有点相当于session。

你可能感兴趣的:(tomcat,Web,jsp,servlet,IE)