日期:2014-05-18  浏览次数:20766 次

servlet基本理解问题-----------热心的人过来帮帮忙
看一下一段最基本的代码
      package   packet1;

import   java.io.IOException;
import   java.io.PrintWriter;

import   javax.servlet.ServletConfig;
import   javax.servlet.ServletException;
import   javax.servlet.http.HttpServlet;
import   javax.servlet.http.HttpServletRequest;
import   javax.servlet.http.HttpServletResponse;

public   class   Servlet1   extends   HttpServlet   {
int   count;
public   Servlet1()   {
super();
}

public   void   destroy()   {
super.destroy();   //   Just   puts   "destroy "   string   in   log
//   Put   your   code   here
}


public   void   doGet(HttpServletRequest   request,   HttpServletResponse   response)
throws   ServletException,   IOException   {

response.setContentType( "text/html;charset=GBK ");
PrintWriter   out   =   response.getWriter();
out.println( " <HTML> ");
out.println( "     <HEAD> <TITLE> A   Servlet </TITLE> </HEAD> ");
out.println( "     <BODY> ");
count++;
out.print( "自从加载后(读取初始化参数后,   ");
out.print( "这个Servlet已经访问了 ");
out.println(count+ "次。 ");
out.println( "     </BODY> ");
out.println( " </HTML> ");
out.flush();
out.close();
System.out.println( "该Servlet的doget方法被执行一次 ");
}

public   void   doPost(HttpServletRequest   request,   HttpServletResponse   response)
throws   ServletException,   IOException   {
}

public   void   init(ServletConfig   config)   throws   ServletException   {
//   Put   your   code   here
super.init(config);
String   initial   =   config.getInitParameter( "initial ");
try{
count   =   Integer.parseInt(initial);
}catch(NumberFormatException   e){
count=0;
}
System.out.println( "计数器Servlet已经初始化 ");
}

}

这段代码没有什么问题,我现在的问题就是在理解HttpServletRequest   request,   HttpServletResponse   response这两个参数上,按照书上讲的,HttpServletRequest(HttpServletResponse   )是一个接口,那把接口对象(暂时这样称呼)作为方法的形参也是没有问题,但是在创建servlet实例的时候这个参数究竟是哪一个类的对象的,因为按照JAVA的对接口的定义,接口所有方法必须是抽象的,那抽象方法是不可以有对象的,请教各位高手,创建servlet实例的时候,究竟是什么类实例化了HttpServletResponse   /HttpServletRequest接口?


------解决方案--------------------
自己查看tomcat源代码,其实就是容器用的wrapper来对请求和回应的包装。
------解决方案--------------------
全给我好了....