请教servlet的最粗浅问题。(20分)

S

serial0

Unregistered / Unconfirmed
GUEST, unregistred user!
我在2000上安装了j2sdk 1.4 + tomcat 4.0.3。
运行http://local:8080/ 能看到网页,能运行所带的各个例子,一切正常。
1、不过,我找了半天,也没有发现类路径这个变量。
  autoexec.bat中没有。系统的环境变量中也没有classpath这个变量。这是什么原因?
2、如果我写servlet,是不是要先写好存为.java文件,再用javac.exe编译它,
  然后再将.class放在tomcat的webapps下的一个目录中(如examples/web-inf/classes),
当IE访问时servlet时,调用的是.class文件;还是说只要把.java文件,放到目录下就可以了。
3、在j2sdk中是不是没有包括javax.servlet这个包。
如果我想用javac.exe来编译servlet的.java文件,那应该怎么做?
 是要手工设置一个classpath的系统变量,来指向tomcat下面的servlet.jar文件?
 
1,java程序只有命令行的'java'才会使用系统的classpath,其余的程序,都是自己选择是不是使用
这个系统的classpath. tomcat就不是,他自己有一套classpath. 一般servlet container 对付
classpath,都是这样的:servlet container本身的classpath,在配置文件中指定;servlet container
也会把某一个目录,比如/lib/ext下的jar自动加入所有web-app的classpath.web app使用的classpath,
还会加上这个web-app的web-inf/lib下的jar和zip,再加上web-inf/classes
2, 对。某些servlet container会自己编译java.这时候要注意你使用的servlet container
的文档中是如何描述的。不同的servlet contain的处理方式各不相同。我曾经在jrun 3遇到过这样的情况:
在web-inf/classes目录下同时放了.java和.class文件,然后再放入一个新版本的.class,结果jrun不采用
这个新版本的.class,它自己拿.java文件编译去了.我的教训就是永远不要把.java文件放到web-inf/classes
目录下。
3.对
 
多谢指点。
上午我找了一下,没找到tomcat文档中关于它如何处理servlet是用.java,还是用.class。
于是,我就把它自带的HelloWorldEXample.java,转存为Hello.java(当然里面的类名也改成Hello),
然后,我去用浏览器去访问这个hello。但是,是出错了,提示没有这个东西。这是否说明tomcat是采用.class来处理
servlet。
后来,我想用eclipse来编译这个hello.java。我在eclipse中新建了一个项目study。在项目中
增加一个Extenral JARS来指向tomcat/common/lib/servlet.jar。在项目的视图中,我展开servlet.jar后,
可以看到相关的HttpServlet等类。但是,我编译hello.java时,eclipse还是提示出错,如
HttpServlet cannot be resolved or is not a valid superclass
HttpServletRequest cannot be resolved (or is not a valid type)for the argument request of the methoddo
Get
HttpServletResponse cannot be resolved (or is not a valid type) for the argument response of the methoddo
Get
ServletException cannot be resolved (or is not an exception type) for the methoddo
Get
ResourceBundle cannot be resolved or is not a type
是不是在这项目中,还需要引入tomcat中其他相关的类(包),还是servlet.jar没引用对?
我用的是eclipse v2.0 build 20020521
还请曹大侠指点一二。谢谢。
 
Sorry。
问题已经解决了。我犯一个不可以原谅的“低级’错误 ,我拷贝原来的.java文件中,
把上面的import给忘记了。刚才重新加入后,可以成功编译,可以显示hello了。
刚才我把hello.class放到webapps/exmaples/web-inf/classes/目录下,删除了hello.java。显示正常。
看来tomcat是认.class的。
 
接受答案了.
 
顶部