jsp servlet 学习

JSP / SERVLET

1.         Discussion

A servlet is a Java class that is designed to respond with dynamic(动态的) content to client requests over a network. If you are familiar with Common Gateway Interface (CGI) programs, then servlets are a Java technology that can replace CGI programs. Often called a web component (along with JSPs), a servlet is executed within a runtime environment provided by a servlet container or web container such as Jakarta Tomcat or BEA WebLogic.

    A web container can be an add on component to an HTTP server, or it can be a standalone server such as Tomcat, which is capable of managing HTTP requests for both static content (HTML files) as well as for servlets and JSPs.

    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 as a .war file.

  2.         ServletConfig:

An object that implements the javax.servlet.ServletConfig interface. This object gives the servlet access to initialization parameters declared in the deployment descriptorweb.xmlServletConfig also gives the servlet access to a javax.servlet.ServletContext object, with which the servlet can log messages, dispatch requests to other web components, and get access to other web resources in the same application

对于ServletConfig对象来说,与ServletContext相比,它有很大的局限性。当ServletConfig对象在servlet中被实例化后,对任何客户端在任何时候访问有效。但是一个servletServletConfig对象不能被另一个servlet访问,也就是说,在本servlet声明后的ServletConfig只能在本servlet内被访问,属于内部持久有效的变量。

3.         ServletContext:

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 marked "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.

对于web容器来说,ServletContext接口定义了一个servlet环境对象,这个对象定义了一个在servlet引擎上的servlet的视图。通过使用这个对象,servlet可以记录事件,得到资源并得到来自servlet的引擎类。servlet容器在启动时会加载web应用,并为每个web应用创建唯一的servlet context对象,可以把ServletContext看成是一个Web应用的服务器端组件的共享内存,在ServletContext中可以存放共享数据,他提供了4个读取和设置共享数据的方法。具体见api帮助文档。另外,ServletContext对象只在web应用被关闭的时候才被销毁,不同的web应用,ServletContext各自独立存在。

一个web应用由jsp,servletjavabeanweb组件的集合构成,每一个web应用,容器都会有一个背景对象,而javax.servlet.ServletContext接口就提供了访问这个背景对象的途径。你可以通过一个servlet实例的getServletContext()方法得到该servlet运行其中的这个背景对象,从这个背景对象中你可以访问如下信息资源:

1.       初始化参数

2.       存储在背景中的对象

3.       与背景关联的资源

4.       日志

最后针对ServletContext的总结:

ServletContextservlet容器,其内提供的方法可以在同一web应用下的所有servlet中被使用

4.         Servlet's lifecycle

a)        The servlet container calls the servlet's init( ) method, which is designed to initialize resources that the servlet might use, such as a logger when the web server starts or the Servlet is requested by client first time . The init( ) method gets called only once during the servlet's lifetime.

b)        The init( ) method initializes an object that implements the javax.servlet.ServletConfig interface. This object gives the servlet access to initialization parameters declared in the deployment descriptor. ServletConfig also gives the servlet access to a javax.servlet.ServletContext object, with which the servlet can log messages, dispatch requests to other web components, and get access to other web resources in the same application

Servlet developers are not required to implement the init( ) method in their HttpServlet subclasses.

c)        The servlet container calls the servlet's service( ) method in response to servlet requests. In terms of HttpServlets, service( ) automatically calls the appropriate HTTP method to handle the request by calling (generally) the servlet's doGet( ) or doPost( ) methods. For example, the servlet responds to a user sending a POST HTTP request with a doPost( ) method execution.

d)        When calling the two principal HttpServlet methods, doGet( ) or doPost( ), the servlet container creates javax.servlet.http.HttpServletRequest and HttpServletResponse objects and passes them in as parameters to these request handler methods. HttpServletRequest represents the request; HttpServletResponse encapsulates the servlet's response to the request.

e)        The servlet or web container, not the developer, manages the servlet's lifecycle, or how long an instance of the servlet exists in the JVM to handle requests. When the servlet container is set to remove the servlet from service, it calls the servlet's destroy( ) method, in which the servlet can release any resources, such as a database connection.

 参考:http://www.javaresearch.org/article/60126.htm
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值