0

I have a Song servlet that has some code and then sends the request to a songList.jsp and with this the titles of the songs are listed in a page. And this is working fine. But then, in the songList.jsp i defined a url for each song but when the url is clicked it is appearing:

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 where is the issue?

Song Servlet

@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

Im showing the song in the page and its woking ok

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

This link should go to a page that shows info about a song, and have this format:

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

But when is clicked appears the 404 error.

And then I have a 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);
  }

Then I have the songPage.jsp but should not be important for the issue.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
OzzyW
  • 117
  • 3
  • 14

1 Answers1

2

Indeed It should be 404. @WebServlet(name ="/SongPage", urlPatterns = {"/SongPage/name/*/id/*"}) Compare your urlPatterns of above & your link "http://localhost:8080/SongPage?name=Achtung&id=70". I you have basic idea about how does GET send parameters you would realized the point. ?name=Achtung&id=70belongs to parameters of GET request.

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

GrootC
  • 113
  • 2
  • 10
  • Thanks,first i used like that but it appears the 404 error, so I was looking for a solution and test with {"/SongPage/name/*/id/*"}) but also dont Works. – OzzyW Dec 19 '17 at 17:51
  • @OzzyW {"/SongPage/name/*/id/*"} is totally wrong, Did you check web.xml servlet mappings ? I'm wondering why don't you put `@Override` ? – GrootC Dec 19 '17 at 17:59
  • Thanks for your answer. In web.xml i dont have any mapping. Im using the urlPatterns, is the same right? And @override in which part? – OzzyW Dec 19 '17 at 18:03
  • Yeah. It's fine.. `@Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {` – GrootC Dec 19 '17 at 18:10
  • Thanks, but it appears the same issue. – OzzyW Dec 19 '17 at 18:13
  • Thanks, the Song Servlet is in the same folder of SongPage servlet, also songList is in the same folder of songPage.jsp so it also should be ok because Song servlet and songList.jsp works. – OzzyW Dec 19 '17 at 18:21
  • Consider about relative URL, make sure about your paths. http://localhost:8080//SongPage , This is the way of accessing your SongPage class. It's not a big isuue, look carefully about your paths – GrootC Dec 19 '17 at 18:32