如何理解应用上下文ServletContextrequest:获得请求中的数据
response:输出数据serveletContext:有是什么数据 如何理解

解决方案 »

  1.   

    Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file. There is one context per "web application" per Java Virtual Machine. (A "web application" is a collection of servlets and content installed under a specific subset of the server's URL namespace such as /catalog and possibly installed via a .war file.) In the case of a web application ed "distributed" in its deployment descriptor, there will be one context instance for each virtual machine. In this situation, the context cannot be used as a location to share global information (because the information won't be truly global). Use an external resource like a database instead. The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized. 
      

  2.   

    你在Eclipse中,看下 serveletContext 有多少方法,每个方法是干什么的。或者直接看API说明。就知道这个变量时干什么的。不过现在都用框架,很少直接用到这个类了。
      

  3.   

    request和servletContext都就容器,只是生命周期不同,request是在一个请求过程中可存储参数等,servletContext是在整个工程运行期都有效。
    request中的值在一次请求结束后就不存在了,存在servletContext中的值一直存在,直到tomcat停止。
      

  4.   

    每个web应用会有一个唯一的serveletContext对象。
    可以存取application范围的数据。
    可以获取当前web应用资源,比如取得severlet配置中配置的参数。
    可以获取应用路径。
      

  5.   

    楼上,可以获取当前web应用资源,比如取得severlet配置中配置的参数。不是通过servletConfig这个吗??
      

  6.   

    可以把serveletContext理解成全局数据,servlet和jsp可以调用,但是非线程安全,用的时候需要注意
      

  7.   

    http://blog.csdn.net/bryanliu1982/archive/2010/01/19/5214899.aspx   说得比较细
      

  8.   

    自己理解只能通过实际的应用去理解了,比如:要将数据库中的显示到页面上,首先肯定要发送请求到服务器,服务器查好后就要将结果送到前台页面,方法就是将查到的结果放在list里然后request.setAttribute("list",list)存到request中,在页面从request中取出即可,请求结束后request就会清空.
    如果是计数网站的访问次数,就不能用request,因为每次都会从0开始,而如果放在servletContext中是一直存在于服务器上的,每次访问一次就会在原来基础上加1.总之,如果是一次请求想临时传个数据就用request,传完后自动会清除,系统负担小,如果是系统常用的数据,如统计等一些数据,需要保留的就放在servletContext 中.