tomcat是如何实现ServletConfig接口的, 在什么地方可以查看到源代码?
我对ServletConfig接口的实现一直比较迷茫,想通过了解tomcat是如何对它做出实现来进一步认识ServletConfig.
[color=darkred][b]在构建一个servlet的时候需要容器提供一个ServletConfig对象,这个servletConfig对象的代码是如何实现的呢?主要就是servletConfig中的几个方法的实现代码!如果有地方可以查看源码,那是最好不过了![/b][/color]
查看servlet API相关信息如下:
javax.servlet Interface ServletConfig 有如下方法:
[color=blue][b]String getInitParameter(String name)[/b][/color]
Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
[color=blue][b]Enumeration getInitParameterNames() [/b][/color]
Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
[color=blue][b]ServletContext getServletContext() [/b][/color]
Returns a reference to the ServletContext in which the caller is executing.
[color=blue][b]String getServletName() [/b][/color]
Returns the name of this servlet instance
javax.servlet Interface Servlet 有如下几个方法:
[color=blue][b] void destroy()[/b][/color]
Called by the servlet container to indicate to a servlet that the servlet is being taken out of service.
[color=blue][b]ServletConfig getServletConfig() [/b][/color]
Returns a ServletConfig object, which contains initialization and startup parameters for this servlet.
[color=blue][b] String getServletInfo() [/b][/color]
Returns information about the servlet, such as author, version, and copyright.
[color=blue][b] void init(ServletConfig config) [/b][/color]
Called by the servlet container to indicate to a servlet that the servlet is being placed into service.
[color=blue][b] void service(ServletRequest req, ServletResponse res) [/b][/color]
Called by the servlet container to allow the servlet to respond to a request.
兄弟 英文好吗?英文好并且有时间的话 建议你看看How Tomcat Works这本书,完全教你Tomcat工作原理以及里面各个组件的分析
首先,你要搞清楚servletConfig和servletContext的区别。
大致是一个serveletConfig专门为一个servlet服务,它里面封装了这个servelet所需的一些常量参数。
一个Servlet容器一般只有一个servletContext,它是为容器中所有的servlet服务的。具体详细信息google查吧
弄清楚了,servletConfig你也明白了。如果要看源码实现,行,去看tomcat的源码吧,tomcat servlet容器帮你实现了这个接口,具体逻辑大致是解析你的servlet配置文件,获得并封装一些常量
tomcat是开源的,去下源代码吧