java - 将另一个包中的 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/fetchData
or ,得到我http://localhost:8080/ProjectName/fetchData
or 。我经常做http://localhost:8080/FetchServlet/fetchData
404
405
mvn 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>
解决方案
推荐阅读
- c# - 如何访问 Linq2db 中的 Oracle 链接表?
- java - 在 Minecraft Mod 中发送 HTTP 请求
- php - 如果不在预览模式下,wp_customize 会呈现空的 CSS 声明
- owl - 在 OWL-API 中解析曼彻斯特语法的 EOF 错误
- angular - 对象可能是“空”(未定义)
- python - 运行时错误会话关闭 Python Pepper 机器人
- android-jetpack-compose - Jetpack 导航撰写,底部导航,制作后退按钮切换标签?
- tensorflow - Input_from Conv_1D 信号数据
- c# - 如何将 MenuItem 资源添加到现有的 MenuItem 控件模板?
- php - While循环在显示之前不等待