【问题标题】:Servlet 3.0 HTTP 404 - The requested resource () is not available [duplicate]Servlet 3.0 HTTP 404 - 请求的资源()不可用[重复]
【发布时间】:2012-11-17 13:24:37
【问题描述】:

环境:

  • Ubuntu 12.04
  • Apache Tomcat 7.0.27
  • Eclipse 4.2 (Juno)
  • Servlet 3.0
  • OpenJDK 7

尝试使用 Servlet 3.0 执行 FileCounter,我在 URL 上收到此错误

http://localhost:8080/wtp.filecounter/

不一定是http://localhost:8080/wtp.filecounter/FileCounter

Estado HTTP 404 -

类型通知国家

门萨耶

描述 El recurso requerido () no está disponible.

Apache Tomcat/7.0.26

结构

wtp.filecounter
   dao
      FileDao.java
   servlets
      FileCounter.java

FileDao.java 读取和更新文件的访问者

package wtp.filecounter.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

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 读取计数器并以纯文本形式写入的 servlet

package wtp.filecounter.servlets;
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;
import javax.servlet.http.HttpSession;

import wtp.filecounter.dao.FileDao;

/**
 * Servlet implementation class FileCounter
 */
@WebServlet("/FileCounter")
public class FileCounter extends HttpServlet {
  private static final long serialVersionUID = 1L;

  int count;
  private FileDao dao;

  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());
    }
  }

  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    // Set a cookie for the user, so that the counter does not increate
    // everytime 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.");
  }

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

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>wtp.filecounter</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>
</web-app>

有什么问题吗?

当我在服务器上运行时,项目打开 ''http://localhost:8080/wtp.filecounter/'' 而不是 ''http://localhost:8080/wtp .filecounter/FileCounter'' 并且第二个 URL 有效

【问题讨论】:

标签: tomcat servlets annotations tomcat7 servlet-3.0


【解决方案1】:

您的 servlet 映射到 /FileCounter(这就是 @WebServlet("/FileCounter") 注释的作用)。它是 webapp 的一部分,部署在某个上下文路径下(假设上下文路径是 /myFirstWebApp)。所以调用 servlet 的 URL 是:

http://localhost:8080/myFirstWebApp/FileCounter

【讨论】:

  • 上下文路径在哪里定义?因为我所做的是在服务器上运行的,localhost:8080/wtp.filecounter 是我得到的 URL 错误。它错过的是 /FileCounter
  • 在eclipse中选择你的web项目,选择Project - Properties,进入“Web project settings”,你会找到context root。
  • 我遵循这个例子,但是改变了 servlet 3.0 vogella.com/articles/EclipseWTP/article.html 并且没有任何东西被定义为上下文根并且它工作了¿?
  • 默认情况下,context root是部署在tomcat中的war文件的名称,也是eclipse项目的名称,AFAIK。
  • 我遗漏了一些东西,因为在他的示例中,项目名为 de.vogella.wtp.filecounter 并且 servlet 的 URL 是 de.vogella.wtp.filecounter/FileCounter (但在他的情况下servlet 2.5) eclipse 自动将 servlet 设置添加到 web.xml 文件中,所以我们不知道里面有什么
【解决方案2】:

您的应用程序的名称是wtp.filecounter,并且您有一个映射"/FileCounder" 的servlet。这就是为什么如果你打 "http://localhost:8080/wtp.filecounter/FileCounter" 在您的浏览器中,映射 "/FileCounter" 的 servlet 的 doGet 方法被调用并得到响应。

【讨论】:

    【解决方案3】:

    'http://localhost:8080/wtp.filecounter/FileCounter' 有效,因为 servlet 在那里找到。

    “wtp.filecounter”部分是您的应用程序上下文。如果您想使用“http://localhost:8080/wtp.filecounter/”访问 servlet,那么您需要将欢迎文件更改为指向 FileCounter servlet,而不是使用那些默认的欢迎文件.

    【讨论】:

    【解决方案4】:

    只需将 FileCounter 添加为 web.xml 中的“欢迎文件”之一

    【讨论】:

      【解决方案5】:

      您所指的教程是使用动态 Web 模块 2.5 版,而您可能使用的是 3.0。 仔细查看您的 web.xml,您会发现那里没有创建任何 servlet 映射。如果您不使用 eclipse 的默认 servlet 创建工具,可能会发生这种情况。这是我的 web.xml 的样子:

      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://java.sun.com/xml/ns/javaee"
          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
          id="WebApp_ID" version="3.0">
          <display-name>com.vogella.web.filecounter</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.vogella.web.filecounter.servlet.FileCounter</servlet-class> 
          </servlet>
      
          <servlet-mapping>
              <servlet-name>FileCounter</servlet-name>
              <url-pattern>/FileCounter</url-pattern>
          </servlet-mapping>
      </web-app>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-02
        • 2020-04-29
        • 2016-09-18
        • 1970-01-01
        • 2021-01-06
        • 1970-01-01
        • 2011-06-26
        • 2021-07-27
        相关资源
        最近更新 更多