Win a copy of Mastering Non-Functional Requirements this week in the Design forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to send which html link was clicked to the servlet?  RSS feed

 
Cedric Key
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, so I read this: https://coderanch.com/t/280802/java/Knowning-link-clicked

Kind of helpful, but it's JSP, which I have no experience in and I don't even know what it is to begin. So I'm wondering what the difference is with just plain HTML?

I have a database with a table of movies, with unique IDs and the movie name, year, and ranking stored inside. I retrieve the latter three and make an HTML table containing them, then insert that String into a .ftl file. However, I want to make the movie names link to another .ftl (movie.ftl), but I need to somehow retrieve a unique identifier from each link so that I can know which movie was clicked on and generate the movie.ftl file accordingly (which will contain list of actors, director, and such stuff). I could use a form and buttons instead but links look much cleaner, so if this is possible that will be nice.

The first column of the movie table has the unique ID, the second column has the movie name. So looping through the ResultSet would look something like...?



And the htmlstring would then be put inside these tags in the .ftl file:


But I'm not really sure what to do in MovServlet. Do I have to check for every number with request.getParameter("id") until I get a match for the id of the clicked link? It seems tedious and inefficient...and plus the id numbers of the movies are a bit all over the place, like there are movie ids that range from 2026 to 9749 and since some movies have been deleted from the table there are a lot of gaps between.
Surely there is a better way?
 
Paul Clapham
Sheriff
Posts: 22215
38
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cedric Key wrote:But I'm not really sure what to do in MovServlet. Do I have to check for every number with request.getParameter("id") until I get a match for the id of the clicked link? It seems tedious and inefficient...and plus the id numbers of the movies are a bit all over the place, like there are movie ids that range from 2026 to 9749 and since some movies have been deleted from the table there are a lot of gaps between.
Surely there is a better way?


I'm confused about your confusing. If the user clicks on the HTML link which looks like <a href="MovServlet?id=4242"/> then MovServlet's doGet() method is called and the value of the "id" request parameter is 4242. I don't understand what it is you think you have to check 4242 against. If you want to get the database record for that movie, you'd just do an SQL SELECT statement with a suitable WHERE clause to select the row with ID=4242.
 
Paul Clapham
Sheriff
Posts: 22215
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Cedric Key
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh...um, I think I had a totally durr moment there. Thank you, haha (for the welcome and the help)! The people here seem to be much more kind and noob-tolerant than those at stackoverflow so I think I'm switching over ;; So other than that, what I've got is good to go?
 
Paul Clapham
Sheriff
Posts: 22215
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's probably not good to go, actually. The code you posted which generates "htmlstring" looks like Java, but then it shows up in something which looks like JSP. You should really be generating your desired HTML (which does look reasonably good to me) in JSP code as well. Otherwise (if I'm not mistaken) your HTML will appear to the user as text instead of as HTML elements. But since you say you know nothing about JSP, I don't know what to say now except that you should find out about JSP.
 
Cedric Key
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've actually already got the table printing out fine (the HTML works! whoo). The file that the form and htmlstring is being put into is an .ftl - I'm using FreeMarker to make a template, and then adding a SimpleHash to the template, which one of the keys is "htmlstring" and its value is the string containing the html table, which I can then access with the ${htmlstring} in the .ftl, and FreeMarker generates an html file from that (my vague understanding). FreeMarker is just the thing my professor introduced us to, is JSP very similar? Just another library(?) to create dynamic web pages with?
 
Paul Clapham
Sheriff
Posts: 22215
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, yes, JSP is like Freemarker in that both are tools for generating HTML. But I wouldn't refer to JSP as a "library", it's more of a language.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!