OzzyW
OzzyW

Reputation: 117

servlet url pattern with parameters issue

When I access "http://localhost:8080/project/Song", dont appears any error, it works it lists some songs in the page. When I click on a song I go to a URL "http://localhost:8080/SongPage?name=A+Tribe&id=201" but here it appears a 404 error, maybe because the url has parameters, accessing this URL appears an error:

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.

Do you know why?

Maybe its because of this urlPattern below:

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

I have this servlet below:

@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);    

    }
}

In songList.jsp i have this:

<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("+"," ");

    ...
 }

Upvotes: 2

Views: 1076

Answers (1)

tsolakp
tsolakp

Reputation: 5948

Your original url "http://localhost:8080/project/Song" has "project" as base path but the other url "http://localhost:8080/SongPage?name=A+Tribe&id=201" does not have that path. Most likely you need to update your JSP to include it in the "href" tag as a quick fix:

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

or most likely if it is actually a context path:

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

Upvotes: 2

Related Questions