首页 > 代码库 > Servlet(二)GenericServlet

Servlet(二)GenericServlet

以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


Servlet(二)GenericServlet