0

这里的代码:

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以及Jetty 9 附带的“javax:javaee-api:jar:6.0” 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
4

2 回答 2

3

这个包是servlet-api.jar的子包,你可以在容器的lib文件夹中找到这个jar文件。对于tomcat是 【tomcat安装目录】->lib->servlet-api.jar

于 2012-12-13T20:42:37.463 回答
0

我建议看一下web servlet api 的更新示例

尽管如此,如果您仍在寻找 2008 年示例使用的库,我会根据Jarvana向您推荐这些库

Maven:  

<dependency>
   <groupId>org.mortbay.jetty</groupId>
   <artifactId>servlet-annotation-spec</artifactId>
   <version>3.0.pre0</version>
</dependency>

奖励:您的 GET 注释似乎属于另一个包

于 2012-12-13T21:18:15.570 回答