这里是代码:
package de.swt1321.servlet;
import java.io.OutputStream;
import javax.servlet.http.annotation.Servlet;
import javax.servlet.http.annotation.GET;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Servlet(urlMappings={"/ServletTest"})
public class ServletTest {
private static final java.nio.charset.Charset UTF8 = java.nio.charset.Charset.forName("UTF8");
@GET
public void handleGet(HttpServletRequest req,
HttpServletResponse res)
{
byte[] HTML = "<html><head><title>Hello World!</title></head><body><h1>IT WORKED!</h1></body></html>".getBytes(UTF8);
res.setStatus(HttpServletResponse.SC_OK);
res.setHeader("content-type","text/html;charset=utf8");
res.setIntHeader("content-length",HTML.length);
OutputStream os = res.getOutputStream();
os.write(HTML);
os.flush();
}
}
我希望这可以根据this工作
,遗憾的是我到目前为止还没找到包含javax.servlet.http.annotation
包的jar。我查看了来自http://download.java.net/maven/2的“javax:javaee-api:jar:6.0”以及Jetty 9附带的servlet-api-3.0.jar
,但到目前为止还没有运气。我在这里有点想法,我缺少什么?
到目前为止,我正在使用此Buildr构建文件构建/尝试构建:
# Version number for this release
VERSION_NUMBER = "1.0.0"
# Group identifier for your projects
GROUP = "swt1321"
COPYRIGHT = ""
# Specify Maven 2.0 remote repositories here, like this:
repositories.remote << "http://repo1.maven.org/maven2"
# This really bugs me, this isn't only supposed to build on my own PC after all!
# But as I said, the one at the download.java.net repo didn't work
JAVA_EE_PATH = "/home/hannes/Lib/Java/jetty-distribution-9.0.0.M3/lib/servlet-api-3.0.jar"
java_ee = artifact("de.swt1321:java_ee:jar:1.0").from(file JAVA_EE_PATH)
project_layout = Layout.new
project_layout[:source,:main,:java] = 'src'
project_layout[:source,:test,:java] = 'test'
desc "The Servlettest project"
define "ServletTest", :layout => project_layout do
project.version = VERSION_NUMBER
project.group = GROUP
manifest["Implementation-Vendor"] = COPYRIGHT
compile.with java_ee
package :war
end
答案 0 :(得分:3)
这个包是servlet-api.jar的子包,你可以在容器的lib文件夹中找到这个jar文件。对于tomcat来说 [tomcat安装目录] - &gt; lib-&gt; servlet-api.jar
答案 1 :(得分:0)
我建议您快速浏览一下more updated example网络servlet api
尽管如此,如果您仍在寻找2008年示例中使用的库,我会根据Jarvana
建议您使用这些库。Maven:
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-annotation-spec</artifactId>
<version>3.0.pre0</version>
</dependency>
BONUS:您的GET注释似乎属于another package