首页 > 解决方案 > 将另一个包中的 Servlet 添加到 Tomcat 的 Web 应用程序的主入口点?

问题描述

以编程方式将 servlet-controller 添加到 Tomcat 服务器后,我无法点击它。

我有这段代码有效。但是,我希望我的Main类位于单独的包中,而我的所有 servlet 都位于另一个包中,并且只需使用new(inside Main) 实例化它们,而不是以下方式:

public static void main(String[] args) throws LifecycleException {
        
        Tomcat tomcat = new Tomcat();
        tomcat.setBaseDir("temp");
        tomcat.setPort(8080);
         
        String contextPath = "/"; 
        String docBase =  new File(".").getAbsolutePath();
         
        Context context = tomcat.addContext(contextPath, docBase);
        HttpServlet fetchServlet = new HttpServlet() {
       
                        private static final long serialVersionUID = 1L;

                        @Override
                        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                                throws ServletException, IOException {
                            PrintWriter writer = resp.getWriter();
                             
                            writer.println("<html><title>Welcome</title><body>");
                            writer.println("<h1>Have a Great Day!</h1>");
                            writer.println("</body></html>");
                        }
        };
                            
        String servletName = "FetchServlet";
        String urlPattern = "/fetchData";

        tomcat.addServlet(contextPath, servletName, fetchServlet);      
        context.addServletMappingDecoded(urlPattern, servletName);
        
        /*
         * Here we can place more servlets and mappings.
         */
        
        tomcat.start();
        tomcat.getServer().await();

    }

}

mvn exec:java -Dexec.mainClass=path.to.package.Main我通过在浏览器中输入来启动 Tomcat,http://localhost:8080/fetchData我得到了响应。

但是,如果我使用以下代码启动 Tomcat:

 public static void main(String[] args) throws LifecycleException {
        
        Tomcat tomcat = new Tomcat();
        tomcat.setBaseDir("temp");
        tomcat.setPort(8080);
         
        String contextPath = "/"; 
        String docBase =  new File(".").getAbsolutePath();
         
        Context context = tomcat.addContext(contextPath, docBase);

                
       HttpServlet fetchServlet = new FetchServlet();
        
        String servletName = "FetchServlet";
        String urlPattern = "/fetchData";

        tomcat.addServlet(contextPath, servletName, fetchServlet);      
        context.addServletMappingDecoded(urlPattern, servletName);
        
        /*
         * Here we can place more servlets and mappings. 
         */
        
        tomcat.start();
        tomcat.getServer().await();

    }

}

在这种情况下,启动 Tomcatmvn exec:java -Dexec.mainClass=path.to.package.Main并在浏览器中输入http://localhost:8080/fetchDataor ,得到我http://localhost:8080/ProjectName/fetchDataor 。我经常做http://localhost:8080/FetchServlet/fetchData404405mvn clean install

如何制定第二种工作方式?

此外,我寻找的每个地方(http://home.apache.org/~markt/presentations/2010-11-04-Embedding-Tomcat.pdf [幻灯片 18];https://www.programcreek.com/java -api-examples/?class=org.apache.catalina.startup.Tomcat&method=addServlet;还有其他几个地方),他们像第二种方式一样实例化servlet。

我正在使用嵌入式 tomcat,这是一个简单的 Eclipse 项目。这里是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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.company</groupId>
  <artifactId>assignment</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>AssignmentApp</name>
  <description>Assignment app for Internship Company</description>
  
   <properties>
     <tomcat.version>8.0.48</tomcat.version>
   </properties>
  
    <dependencies>
  
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-core</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <version>${tomcat.version}</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-logging-juli</artifactId>
        <version>${tomcat.version}</version>
    </dependency>

  </dependencies>
  
  <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>2.0.0</version>
            <configuration>
                <assembleDirectory>target</assembleDirectory>
                <programs>
                    <program>
                        <mainClass>path.to.package.Main</mainClass>
                    </program>
                </programs>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>assemble</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
  </build>
  
</project>

标签: javatomcatservletsembedded-tomcat-7

解决方案


推荐阅读