Servlet(二)GenericServlet

时间:2022-01-09 17:47:17

以GenericServlet创建Servlet

1、在FirstServlet同文件夹下建立GServlet.java

package com.hunhun;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class GServlet extends GenericServlet{ public void service(ServletRequest req,
ServletResponse res)
throws ServletException,
IOException{ res.getWriter().println("GenericServlet "+new Date().toLocaleString()); }
}

2、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_3_0.xsd"
version="3.0"
metadata-complete="true">
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.hunhun.FirstServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
<url-pattern>/FirstServlet</url-pattern>
</servlet-mapping> <servlet>
<servlet-name>GServlet</servlet-name>
<servlet-class>com.hunhun.GServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>GServlet</servlet-name>
<url-pattern>/GServlet</url-pattern>
</servlet-mapping> </web-app>

3、编译文件,reload myweb项目,注意每次又一次改动*Servlet.java文件,都须要又一次编译,并又一次载入 myweb项目或重新启动tomcat7

4、浏览器訪问 http://localhost:8080/myweb/GServlet,可看到

GenericServlet 2014-11-14 21:20:39