ServletContainerInitializer

ServletContainerInitializer

package sampleJSN;

import java.io.IOException;
import java.util.Set;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Init implements ServletContainerInitializer {

    @SuppressWarnings("serial")
    @Override
    public void onStartup(Set<Class<?>> arg0, ServletContext arg1) throws ServletException {
        System.out.println("ssd");
        HttpServlet serv = new HttpServlet() {
            @Override
            protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                    throws ServletException, IOException {
                resp.getWriter().write("<b>JSN</b>");
            }

            @Override
            public void init(ServletConfig config) throws ServletException {
                System.out.println("sirappu");
                super.init(config);
            }
        };
        Dynamic dyc = arg1.addServlet("Name", serv);
        dyc.addMapping("/jsn");
    }

}


我正在编写一个程序来测试ServletContainerInitializer。问题是ServletContainerInitializer本身可以正常工作,并且ServletContainerInitializer.onStartup被打印为“ ssd”,但是ServletContext.addServlet没有注册我的servlet,因此没有调用init方法。请检查下面的日志

    Apr 11, 2018 12:18:45 AM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:CheckWeb' did not find a matching property.
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version:        Apache Tomcat/8.0.36
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built:          Jun 9 2016 13:55:50 UTC
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server number:         8.0.36.0
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name:               Windows 8.1
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version:            6.3
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture:          amd64
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home:             C:\Program Files\Java\jdk1.8.0_161\jre
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version:           1.8.0_161-b12
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor:            Oracle Corporation
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE:         C:\Users\BalajiGold\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME:         C:\tomcat-8
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:63748
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=C:\Users\BalajiGold\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=C:\tomcat-8
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dwtp.deploy=C:\Users\BalajiGold\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.endorsed.dirs=C:\tomcat-8\endorsed
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dfile.encoding=Cp1252
Apr 11, 2018 12:18:45 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.8.0_161\jre\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Program Files\Java\jdk1.8.0_161\jre\bin;C:/Program Files/Java/jdk1.8.0_121/jre/bin/server;C:/Program Files/Java/jdk1.8.0_121/jre/bin;C:/Program Files/Java/jdk1.8.0_121/jre/lib/amd64;C:\Program Files\Microsoft MPI\Bin\;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\system32;C:\Windows;C:\Program Files\QuickTime\QTSystem;C:\cygwin64\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\MinGW\bin;C:\MinGW\MSYS\1.0\bin;C:\Program Files\Git\bin;C:\Users\BalajiGold\AppData\Local\Programs\Eclipse Che\eclipse-che-5.0.0\tools\jre\bin;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\ManagementStudio\;C:\Program Files\Java\jdk1.8.0_161\bin;C:\Program Files\Docker Toolbox;C:\apache-maven-3.5.2\bin;C:\Users\BalajiGold\Desktop;;.
Apr 11, 2018 12:18:45 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Apr 11, 2018 12:18:45 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Apr 11, 2018 12:18:45 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Apr 11, 2018 12:18:45 AM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Apr 11, 2018 12:18:45 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 941 ms
Apr 11, 2018 12:18:45 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Apr 11, 2018 12:18:45 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.36
Apr 11, 2018 12:18:48 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
Apr 11, 2018 12:18:50 AM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
ssd
Apr 11, 2018 12:18:54 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Apr 11, 2018 12:18:54 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Apr 11, 2018 12:18:54 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 9179 ms

最佳答案

dyc.setLoadOnStartup(0);


该代码为我解决了上述问题

PS:别忘了添加META-INF/services/javax.servlet.ServletContainerInitializer

09-29 22:11