重新学习Servlet
package javax.servlet; import java.io.IOException; public interface Servlet { public void init(ServletConfig config) throws ServletException; public ServletConfig getServletConfig(); public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException; public String getServletInfo(); public void destroy(); }
public abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable
package com.xh.test.api; import javax.servlet.*; import java.io.IOException; /** * Created by root on 17-11-12. */ public class MyGenericServlet extends GenericServlet { public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { System.out.println(">>>service " + req + " , " + res); String id = req.getParameter("id"); System.out.println(">>>id " + id); res.getWriter().write("hello i am MyGenericServlet"); res.flushBuffer(); } @Override public void init(ServletConfig config) throws ServletException { System.out.println(">>>init_p :" + config); super.init(config); } @Override public void init() throws ServletException { System.out.println(">>>init"); super.init(); } @Override public void destroy() { System.out.println(">>>destroy"); super.destroy(); } public MyGenericServlet() { System.out.println(">>>MyGenericServlet"); } }
访问:http://localhost:8080/testservlet/1?id=100
浏览器输出:hello i am MyGenericServlet
控制台输出:
>>>MyGenericServlet
>>>init_p :org.apache.catalina.core.StandardWrapperFacade@4a1957bf
>>>init
>>>service org.apache.catalina.connector.RequestFacade@e4f73de , org.apache.catalina.connector.ResponseFacade@297fd904
>>>id 100
PS:容器启动的时候没有初始化,第一次访问才初始化。init(ServletConfig config)的参数由容器传入。ServletConfig包括当前Servlet的名字,服务器监听的地址等
配置文件:
web.xml:
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>MyGenericServlet</servlet-name> <servlet-class>com.xh.test.api.MyGenericServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MyGenericServlet</servlet-name> <url-pattern>/1</url-pattern> </servlet-mapping> </web-app>
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xh.test.servlet</groupId> <artifactId>testservlet</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>testservlet Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> </dependencies> <build> <plugins> <!-- 本地环境使用 --> <plugin> <groupId>org.apache.tomcat.maven</groupId> <artifactId>tomcat7-maven-plugin</artifactId> <version>2.1</version> <configuration> <hostName>localhost</hostName> <!-- Default: localhost --> <port>8080</port> <!-- 启动端口 Default:8080 --> <path>/${project.artifactId}</path> <!-- 访问应用路径 Default: /${project.artifactId}--> <uriEncoding>UTF-8</uriEncoding> <!-- uri编码 Default: ISO-8859-1 --> </configuration> </plugin> </plugins> <finalName>testservlet</finalName> </build> </project>