我想尝试为Servlet API创建一个Maven项目。我创建了一个名为HelloWorldServlet的servlet,如下所示:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class HelloWorldServlet
*/
@WebServlet("/HelloWord")
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloWorldServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
printWriter.println("<h1>Hello World!</h1>");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("do post method called");
}
}
在构建项目时,它会引发以下错误:
ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /C:/Project_Work/Other_learn/eclipse_workspace_lear/servletapi/src/main/java/com/learn/servlet/HelloWorldServlet.java:[6,21] package javax.servlet does not exist
[ERROR] /C:/Project_Work/Other_learn/eclipse_workspace_lear/servletapi/src/main/java/com/learn/servlet/HelloWorldServlet.java:[7,32] package javax.servlet.annotation does not exist
[ERROR] /C:/Project_Work/Other_learn/eclipse_workspace_lear/servletapi/src/main/java/com/learn/servlet/HelloWorldServlet.java:[8,26] package javax.servlet.http does not exist
[ERROR] /C:/Project_Work/Other_learn/eclipse_workspace_lear/servletapi/src/main/java/com/learn/servlet/HelloWorldServlet.java:[9,26] package javax.servlet.http does not exist
[ERROR] /C:/Project_Work/Other_learn/eclipse_workspace_lear/servletapi/src/main/java/com/learn/servlet/HelloWorldServlet.java:[10,26] package javax.servlet.http does not exist
[ERROR] /C:/Project_Work/Other_learn/eclipse_workspace_lear/servletapi/src/main/java/com/learn/servlet/HelloWorldServlet.java:[16,40] cannot find symbol
symbol: class HttpServlet
我在构建路径中添加了以下jar文件: javax.servlet-api-3.0.1.jar和servlet-api.jar
所以我要求你们让我知道我在这里想念的东西。预先感谢。
答案 0 :(得分:0)
将库手动添加到Eclipse的构建路径不会自动将其添加到maven构建生成的文件中。只有通过在pom文件中添加依赖项来加载该文件时,该文件才会被添加。
因此,这里我们需要遵循两个步骤,首先,将jar文件复制到maven本地存储库,然后将其依赖项添加到pom.xml文件中,如下所示:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>servlet-api-id</id>
<phase>clean</phase>
<goals>
<goal>install-file</goal> // copy file to local maven repo
</goals>
<configuration>
<packaging>jar</packaging>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<file>${project.basedir}/lib/servlet-api.jar</file>
</configuration>
</execution>
</executions>
</plugin>
->,然后添加依赖项:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>