1.编写servlet,在tomcat\webapps目录下新建文件Test,Test目录如下
Test
|
classes web.xml
|
test01
|
Servletfirst.java
Servletfirst.java文件内容如下
package test01; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Servletfirst extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
PrintWriter out=response.getWriter();
out.println("Hello world!");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
doGet(request, response);
}
}
2.在命令行模式下往classpath变量中添加servlet-api.jar的路径,例如set classpath=%classpath%;E:\Program Files\tomcat\lib\servlet-api.jar
进入Servletfirst.java的目录,编译.java文件生成.class文件,javac -d . Servletfirst.java
3.配置web.xml文件
<servlet>
<servlet-name>Servletfirst</servlet-name>
<servlet-class>test01.Servletfirst</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servletfirst</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
4.在tomcat\bin目录下执行startup.bat开启服务器
5.在浏览器地址栏里输入http://localhost:8080/Test/hello就能访问servlet了。