servlet URL模式带有参数问题



当我访问" http://localhost:8080/project/song"时,不要出现任何错误,它可以工作,它列出了页面中的一些歌曲。当我单击一首歌时,我将转到url" http://localhost:8080/songpage?name = a tribe& id = 201",但这里出现404错误,也许是因为URL具有参数,访问此URL出现一个错误:

HTTP状态404 - 找不到

说明原始服务器没有找到目标资源的当前表示形式,也不愿意透露一个存在。

你知道为什么吗?

也许是因为下面的urlpattern:

@webservlet(name =" songpage",urlpatterns = {"/songpage"})

我在下面有此servlet:

@WebServlet(name ="Song", urlPatterns = {"/Song"})
public class Song extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        QueryManager qm = new QueryManager();
        ArrayList<ArrayList<String>> result = qm.Songs();
        qm.closeConnections();
        request.setAttribute("result", result.get(0));
        request.setAttribute("id", result.get(1));
        RequestDispatcher view=request.getRequestDispatcher("songList.jsp");
        view.forward(request,response);    
    }
}

在Songlist.jsp中我有:

<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>

songpage.java:

@WebServlet(name ="SongPage", urlPatterns = {"/SongPage"})
public class SongPage extends HttpServlet {
    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("+"," ");
    ...
 }

您的原始URL" http://localhost:8080/project/song/song ht project"作为基本路径,但其他url" http://localhost:8080/songpage?name?name?= a tribe&amp; id = 201"没有那条路。您很可能需要更新JSP以将其包含在" HREF"标签中,以作为快速修复:

<a href="project/SongPage?name=${result[status.index].replace(" ","+")}&id=${id[status.index]}"> ${result[status.index]} </a> <br />

,或者很可能实际上是上下文路径:

<a href="${pageContext.request.contextPath}/SongPage?name=${result[status.index].replace(" ","+")}&id=${id[status.index]}"> ${result[status.index]} </a> <br />

最新更新