我有以下Servlet。
package com.ser1;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class FileDao
*/
@WebServlet("/FileDao")
public class FileDao extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public FileDao() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
我刚刚在eclipse中创建了这个新的Servlet,但是我了解了@WebServlet(“ / FileDao”)行。有人可以告诉@WebServlet(“ / FileDao”)有什么用,以及如何解决该错误?
这是eclipse显示的错误
@WebServlet(“ / FileDao”)此行在eclipse中显示为错误
WebServlet无法解析为类型对于注释类型WebServlet,属性值未定义
最佳答案
您需要导入适当的注释:
import javax.servlet.annotation.WebServlet
并将
servlet3.jar
作为编译时间依赖项导入到您的项目中。不要将其复制到战争中,否则它将破坏部署(或做一些奇怪的事情)。可以从Web容器(tomcat)或Oracle复制Jar。该注释用于以编程方式定义servlet。这比在
web.xml
中编写cca 8 xml标记更快,更方便。请参见oracle tutorial。关于java - 无法解决Servlet中的@WebServlet(“/FileDao”)错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22166472/