一段java代码,我不是很理解,希望大家帮我看下解释下,很是感谢,急

来源:百度知道 编辑:UC知道 时间:2024/05/22 18:23:21
1:
public void init() throws ServletException
{
ServletContext cnt = this.getServletContext();
ContextObject obj = new ContextObject();
obj.starTimeStamp();
cnt.setAttribute("obj",obj);
}
-------------------------------------------------------
2:
public void init(ServletConfig cnf) throws ServletException
{
super.init(cnf);
ServletContext cnt = cnf.getServletContext();
ContextObject obj = new ContextObject();
obj.starTimeStamp();
cnt.setAttribute("obj",obj);
}

这两个的区别是什么,也就是说,我糊涂了,请教!!!
看下我的理解,这是第一个里面的
ServletContext cnt = this.getServletContext(); //这个是得到当前上下文。??
ContextObject obj = new ContextObject(); //这是实例化另一个类。??
obj.starTimeStamp(); // 这里是调用那个累的一个方法,不是这里有疑问
cnt.setAttribute("obj",obj); //这是往ServletContext添加obj对象。
但是就是第二个里的 init(Ser

这个是一个配置的servlet
<servlet>
<servlet-name>ipLogServlet</servlet-name>
<servlet-class>com.allanlxf.servlet.life.IpLogServlet</servlet-class>
<init-param>
<param-name>file1</param-name>
<param-value>c:/iplog.txt</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

下面是这个serlvet的init方法
public void init(ServletConfig config) throws ServletException
{
super.init(config);
String file = config.getInitParameter("file");
if(file == null)
{
System.out.println("lack of config parameter named file");
throw new ServletException("lack of config parameter named file");
}

try
{
logger = new PrintWriter(new FileOutputStream(fil