在以编程方式将servlet控制器添加到tomcat服务器之后,我无法点击它。
我有一段代码可以工作。然而,我想为我的 Main
类将其放在单独的包中,并将我的所有servlet放在另一个包中,然后使用 new
(里面 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
在浏览器中键入 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();
}
}
在这种情况下,用 mvn exec:java -Dexec.mainClass=path.to.package.Main
并在浏览器中键入 http://localhost:8080/fetchData
或 http://localhost:8080/ProjectName/fetchData
或 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>
暂无答案!
目前还没有任何答案,快来回答吧!