标签:
servlet 本质是一段java小程序。将servlet加入到servlet容器。
servlet容器:能够运行servlet的环境(tomcat)
web容器:能够运行web应用的环境(tomcat)
开发servlet
1.写一个sun公司定义的servlet接口
2.将写好的类配置到tomcat中,并配置web.xml
FirstServlet.java
package com.zy;
import java.io.*;
import javax.servlet.*;
public class FirstServlet extends GenericServlet
{
public void service(ServletRequest req, ServletResponse res) throws
ServletException, java.io.IOException {
res.getWriter().write("hello Servlet!");
}
}
将FirstServlet.java放入web应用下WEB-INF下面的classes目录下
配置web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<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">
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.zy.FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/servlet/FirstServlet</url-pattern>
配置访问的虚拟路径http://localhost/servlet/FirstServlet
</servlet-mapping>
</web-app>
标签:
原文地址:http://www.cnblogs.com/superPerfect/p/4295963.html