1,request.getRequestDispatcher 方法全称javax.servlet.ServletRequest.getRequestDispatcher(String)
2,在web.xml中配置url-mapping的时候,需要以/开头,否则tomcat启动过程报错;
3,servlet先调用构造函数,再调用init方法;
不进行http请求tomcat不会初始化servlet,关闭tomcat的时候会调用destroy()方法;
package com.stono.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class TestServlet extends HttpServlet { private static final long serialVersionUID = 1L; public TestServlet() {
System.out.println("com.stono.servlet.TestServlet constructor called");
} @Override
public void init() throws ServletException {
System.out.println("com.stono.servlet.TestServlet.init() called");
} @Override
public void destroy() {
System.out.println("com.stono.servlet.TestServlet.destroy() called");
} @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
RequestDispatcher dispatcher = req.getRequestDispatcher("test2.do");
dispatcher.forward(req, resp); } @Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
}
}
2015-9-22 8:57:40 org.apache.catalina.core.AprLifecycleListener init
信息: Loaded APR based Apache Tomcat Native library 1.1.24 using APR version 1.4.6.
2015-9-22 8:57:40 org.apache.catalina.core.AprLifecycleListener init
信息: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
2015-9-22 8:57:40 org.apache.catalina.core.AprLifecycleListener initializeSSL
信息: OpenSSL successfully initialized (OpenSSL 1.0.1c 10 May 2012)
2015-9-22 8:57:40 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["http-apr-8080"]
2015-9-22 8:57:40 org.apache.coyote.AbstractProtocol init
信息: Initializing ProtocolHandler ["ajp-apr-8009"]
2015-9-22 8:57:40 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 851 ms
2015-9-22 8:57:40 org.apache.catalina.core.StandardService startInternal
信息: Starting service Catalina
2015-9-22 8:57:40 org.apache.catalina.core.StandardEngine startInternal
信息: Starting Servlet Engine: Apache Tomcat/7.0.35
2015-9-22 8:57:40 org.apache.catalina.startup.HostConfig deployDirectory
信息: Deploying web application directory D:\Java\apache-tomcat-7.0.35\webapps\TestBootWeb
2015-9-22 8:57:41 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["http-apr-8080"]
2015-9-22 8:57:41 org.apache.coyote.AbstractProtocol start
信息: Starting ProtocolHandler ["ajp-apr-8009"]
2015-9-22 8:57:41 org.apache.catalina.startup.Catalina start
信息: Server startup in 322 ms
com.stono.servlet.TestServlet constructor called
com.stono.servlet.TestServlet.init() called
do get method called!
2015-9-22 8:57:47 org.apache.catalina.core.StandardServer await
信息: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
2015-9-22 8:57:47 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["http-apr-8080"]
2015-9-22 8:57:47 org.apache.coyote.AbstractProtocol pause
信息: Pausing ProtocolHandler ["ajp-apr-8009"]
2015-9-22 8:57:47 org.apache.catalina.core.StandardService stopInternal
信息: Stopping service Catalina
com.stono.servlet.TestServlet.destroy() called
2015-9-22 8:57:47 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["http-apr-8080"]
2015-9-22 8:57:47 org.apache.coyote.AbstractProtocol stop
信息: Stopping ProtocolHandler ["ajp-apr-8009"]
2015-9-22 8:57:47 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["http-apr-8080"]
2015-9-22 8:57:47 org.apache.coyote.AbstractProtocol destroy
信息: Destroying ProtocolHandler ["ajp-apr-8009"]