[英]Deploy war with Jetty maven plugin
I'm trying to deploy my "Hello world!" 我正在尝试部署“ Hello world!” servlet to Jetty using "mvn jetty:run-war".
使用“ mvn jetty:run-war”将servlet传递给Jetty。 Here is my project structure:
这是我的项目结构:
And here is pom.xml: 这是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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ekropotin.test</groupId>
<artifactId>webapp_jetty_test</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>webapp_jetty_test</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.12.v20130726</version>
<configuration>
<webAppSourceDirectory>${project.basedir}</webAppSourceDirectory>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<descriptor>WEB-INF/web.xml</descriptor>
<contextPath>/hello</contextPath>
</webApp>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
</configuration>
</plugin>
</plugins>
</build>
App.java: App.java:
package com.ekropotin.test;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
public class App implements Servlet {
private ServletConfig config;
public void init(ServletConfig config) throws ServletException {
this.config = config;
}
public void destroy() {
}
public ServletConfig getServletConfig() {
return config;
}
public String getServletInfo() {
return "A Simple Servltet";
}
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head>");
out.println("<title>A Sample Servlet!</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello, World!</h1>");
out.println("</body></html>");
out.close();
}
}
web.xml: web.xml中:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.ekropotin.test.App</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
The problem is that when I run "mvn jetty:run-war" and open "0.0.0.0:8080/hello" in my browser, I get the following: 问题是,当我运行“ mvn jetty:run-war”并在浏览器中打开“ 0.0.0.0:8080/hello”时,得到以下信息:
Instead of servlet execution result ("Hello, World!"). 而不是servlet执行结果(“ Hello,World!”)。
I have spent several days trying to figure out what I did wrong. 我花了几天的时间来弄清楚我做错了什么。 You are my last hope guys!
你是我最后的希望!
Well, finally I found the solution. 好吧,终于我找到了解决方案。
Actually my servlet is available by this address: "0.0.0.0:8080/hello /hello ". 实际上,我的servlet可通过以下地址访问:“ 0.0.0.0:8080/hello / hello ”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.