servlet接收参数 - 状态404

时间:2017-12-19 17:33:59

标签: jsp servlets http-status-code-404

我有一个包含一些代码的Song servlet,然后将请求发送到songList.jsp,这样歌曲的标题就会列在页面中。这工作正常。但是,在songList.jsp中,我为每首歌曲定义了一个网址,但是当点击网址时,它会出现:

HTTP Status 404 – Not Found

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

你知道问题出在哪里吗?

歌曲服务

@WebServlet(name ="Song", urlPatterns = {"/Song"})
// some code
request.setAttribute("result", result);
      // some code then send the request
        RequestDispatcher view=request.getRequestDispatcher("songList.jsp");
        view.forward(request,response);    

// songList.jsp

我正在显示页面中的歌曲并且正在显示其正确

<c:forEach items="${result}" var="item" varStatus="status">

  <a href="/SongPage?name=${item[0].replace(" ","+")}&id=${item[1]}">${item[0]}</a>
  </h4>
<p class="card-text">${item[2]}</p>

</c:forEach>

此链接应转到显示有关歌曲信息的页面,并采用以下格式:

"http://localhost:8080/SongPage?name=Achtung&id=70"

但点击时会出现404错误。

然后我有一个SongPage servlet:

@WebServlet(name ="/SongPage", urlPatterns = {"/SongPage/name/*/id/*"})
public class SongPage extends HttpServlet {

public void init(){

System.out.println("Song Servlet");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name").replace("+"," ");
String id = request.getParameter("id");
QueryManager qm = new QueryManager();
ArrayList<ArrayList<String>> result = qm.getSongInfo(name, id);
qm.closeConnections();
ArrayList<String> songInfo = result.get(0);


  request.setAttribute("result", songInfo);
  RequestDispatcher view=request.getRequestDispatcher("songPage.jsp");
  view.forward(request,response);
  }

然后我有了songPage.jsp,但对于这个问题不应该很重要。

1 个答案:

答案 0 :(得分:2)

确实应该是404. @WebServlet(name ="/SongPage", urlPatterns = {"/SongPage/name/*/id/*"}) 比较您上面和上面的urlPatterns您的链接“http://localhost:8080/SongPage?name=Achtung&id=70”。我有基本的想法,关于GET如何发送参数,你会意识到这一点。 ?name=Achtung&id=70属于GET请求的参数。

尝试@WebServlet(name ="/SongPage", urlPatterns = {"/SongPage"})