日期:2014-05-20  浏览次数:20790 次

JAVA servlet import 报错
import java.io.IOException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.List;


import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;


import com.itcast.manager.BookManager;

import com.itcast.web.entity.Book;


public class ServletBook extends HttpServlet {


public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doPost(request, response);

}


public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String type = request.getParameter("type");

if (type != null && type.trim().equalsIgnoreCase("findById")) {


} else if (type != null && type.trim().equalsIgnoreCase("findAll")) {


} else if (type != null && type.trim().equalsIgnoreCase("deleteById")) {


} else if (type != null && type.trim().equalsIgnoreCase("deleteAll")) {


} else {

List<Book> bs = new ArrayList<Book>();

BookManager bm = BookManager.getInstance();

bs = bm.findAll();

request.getSession().setAttribute("bs", bs);

response.sendRedirect("/day10/listBook.jsp");

}


}

}

 

 

很简单的类 居然第8行 import报错

 

 

An error occurred at line: 8 in the generated java file

Syntax error on token "import", Identifier expected after this token


Stacktrace:] with root cause

 

试过把所有注释掉 只写一个重定向也不行 求解..


------解决方案--------------------
你工程里面没有加入servlet-api.jar 所以无法找到servlet这个包

你需要下载或者到tomcat的lib目录下找到这个 jar包,然后加入到你工程的classpath当中

工程名-右键-buildpath-configuration-add jars 或者add external jars 加入到工程应该就没有这个问题了