错误状态404:源服务器未找到当前表示

时间:2017-12-11 18:51:53

标签: java tomcat servlets

我有一个netbeans项目,结构如下。当我访问localhost:8080 / Project / Song时出现错误:

HTTP状态404

描述:原始服务器未找到目标资源的当前表示。

你知道问题在哪里以及如何纠正吗?

项目结构:

 Project
        Web Pages
            META-INF
            WEB-INF
               web.xml
               lib
                  jstl-1.2.jar 
           songs.jsp

        Source Packages
            data
                Song.java

Song.java:

package data;    
import org.apache.jena.base.Sys;
import javax.servlet.RequestDispatcher;
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 java.io.IOException;
import java.util.ArrayList;


@WebServlet(name = "/Song")
public class Song extends HttpServlet {
    /*protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }*/

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        QueryManager qm = new QueryManager();
        ArrayList<ArrayList<String>> result = qm.getSongs();
        qm.closeConnections();

        request.setAttribute("result", result.get(0));
        request.setAttribute("id", result.get(1));
        RequestDispatcher view=request.getRequestDispatcher("songs.jsp");
        view.forward(request,response);
    }

    public void init(){
        System.out.println("Songs page");
    }
}

songs.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <html>
      <head>
        <link href="style.css" rel="stylesheet">
        <title>Songs List</title>
      </head>
      <body>

        <div id="songs">

          <c:forEach items="${result}" var="item" varStatus="status">
            <a href="/SongPage?name=${result[status.index].replace(" ","+")}&id=${id[status.index]}"> ${result[status.index]} </a> <br />
          </c:forEach>
        </div>

      </body>
    </html>

web xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <servlet>
    <servlet-name>Song</servlet-name>
    <servlet-class>Song</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Song</servlet-name>
    <url-pattern>/Song</url-pattern>
  </servlet-mapping>

1 个答案:

答案 0 :(得分:1)

您正在使用两种servlet映射方法。使用其中之一。 注释方法或web.xml 从web.xml文件中删除servlet映射。手段删除下面部分

<servlet>
    <servlet-name>Song</servlet-name>
    <servlet-class>Song</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>Song</servlet-name>
    <url-pattern>/Song</url-pattern>
  </servlet-mapping>

以下行无效。您需要提供班级的完全限定名称。无论如何,如果你有annotation

,你就不需要这个
<servlet-class>Song</servlet-class>

使用它。

  @WebServlet(name = "Song", urlPatterns = {"/Song"})