通过继承GenericServlet抽象类。只需要重写service()方法即可。(这个知识点了解即可)
package cn.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class MyGenericServlet extends GenericServlet{ @Override
public void service(ServletRequest req, ServletResponse resp)
throws ServletException, IOException {
resp.setCharacterEncoding("GBK");
resp.getWriter().println("中文!");
resp.getWriter().print("hello,我继承了GenericServlet"); }
}
XML部署:
<servlet>
<servlet-name>mygenericservlet</servlet-name>
<servlet-class>cn.servlet.MyGenericServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>mygenericservlet</servlet-name>
<url-pattern>/servlet/MyGenericServlet</url-pattern>
</servlet-mapping>