执行servlet时出错

时间:2016-02-10 01:58:39

标签: java eclipse servlets

大家好

我使用Eclipse开发了一个动态Web项目,因为我正在进行一些测试,当我运行项目时出现以下错误:

  

HTTP状态500 - 实例化servlet类时出错com.jperera.SimpleWebApp.servlet.FileCounter类型异常报告消息实例化servlet类时出错com.jperera.SimpleWebApp.servlet.FileCounter

我要描述一下项目,看看是否有人可以帮我解决这个问题:

项目结构:

Project Structure

FileDao.class:

public class FileDao {

public int getCount() {
    int count = 0;
    // Load the file with the counter
    FileReader fileReader = null;
    BufferedReader bufferedReader = null;
    PrintWriter writer = null;
    try {
      File f = new File("FileCounter.initial");
      if (!f.exists()) {
        f.createNewFile();
        writer = new PrintWriter(new FileWriter(f));
        writer.println(0);
      }
      if (writer != null) {
        writer.close();
      }

      fileReader = new FileReader(f);
      bufferedReader = new BufferedReader(fileReader);
      String initial = bufferedReader.readLine();
      count = Integer.parseInt(initial);
    } catch (Exception ex) {
      if (writer != null) {
        writer.close();
      }
    }
    if (bufferedReader != null) {
      try {
        bufferedReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return count;
  }

public void save(int count) throws Exception {
    FileWriter fileWriter = null;
    PrintWriter printWriter = null;
    fileWriter = new FileWriter("FileCounter.initial");
    printWriter = new PrintWriter(fileWriter);
    printWriter.println(count);

    // make sure to close the file
    if (printWriter != null) {
      printWriter.close();
    }
  }

FileCounter.java:

    @WebServlet("/FileCounter")
public class FileCounter extends HttpServlet {
    private static final long serialVersionUID = 1L;
    int count;
    private FileDao dao;

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        // Set a cookie for the user, so that the counter does not increate
        // every time the user press refresh
        HttpSession session = request.getSession(true);
        // Set the session valid for 5 secs
        session.setMaxInactiveInterval(5);
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        if (session.isNew()) {
            count++;
        }
        out.println("This site has been accessed " + count + " times.");
    }

    @Override
    public void init() throws ServletException {
        dao = new FileDao();
        try {
            count = dao.getCount();
        } catch (Exception e) {
            getServletContext().log("An exception occurred in FileCounter", e);
            throw new ServletException("An exception occurred in FileCounter" + e.getMessage());
        }
    }

    public void destroy() {
        super.destroy();
        try {
            dao.save(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

的web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    id="WebApp_ID" version="3.1">
    <display-name>com.jperera.SimpleWebApp</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>FileCounter</servlet-name>
        <servlet-class>com.jperera.SimpleWebApp.servlet.FileCounter</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FileCounter</servlet-name>
        <url-pattern>/FileCounter</url-pattern>
    </servlet-mapping>
</web-app>

如果有任何帮助,我将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为您正在使用注释和xml映射。 请使用其中任何一个。

例如,您可以从web.xml中删除以下部分

   <servlet>
        <servlet-name>FileCounter</servlet-name>
        <servlet-class>com.jperera.SimpleWebApp.servlet.FileCounter</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FileCounter</servlet-name>
        <url-pattern>/FileCounter</url-pattern>
    </servlet-mapping>

或者你可以保留这个并从filecounter.java中删除以下内容

 @WebServlet("/FileCounter")