右键Application-->Properties-->Java Build Path-->Libraries-->Add External JARs-->servlet-api.jar
从而解决import javax.servlet.*;找不到类库的问题。
当然servlet-api.jar是在Tomcat自带的。安装目录下lib文件夹下可找到。
若没安装Eclipse,需要在CLASSPATH添加servlet-api.jar的目录,然后开始-->运行-->"cmd"进入DOS命令提示框,通过cd + 硬盘目录进入.java文件存放目录,通过javac + 文件名即可编译成功。
>>将编译好的Servlet服务器程序布设在Tomcat目录下
在..TomcatwebappsROOTWEB-INF下新建classes和lib文件夹。将编译好的.class放在classes文件夹下,将servelt-api.rar放在lib文件夹下,将web.xml编辑为如下结构:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>
<servlet>
<servlet-name>EchoForm</servlet-name>
<servlet-class>EchoForm</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EchoForm</servlet-name>
<url-pattern>/servlet/EchoForm</url-pattern>
</servlet-mapping>
</web-app>
在浏览器中输入:http://localhost:8080/servlet/EchoForm即可。