Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is it possible to put a scriptlet inside the href attribute of a <a> tag?

 
luisa torress
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following code


I wanna pass the movieid parameter to another JSP in this case addToQueue.jsp. But I'm not getting it to work.
On the addToQueue.jsp I have this, but is printing blank.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is that you must generate valid HTML.

Do a View Source on what is being sent to the browser and inspect the resulting HTML carefully.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that you're printing to System.out--which is on the server.
 
mark goking
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah, System.out outputs to your console. you should use <%= request.getParameter("movieid") %> instead of <% %>
 
rukmani vijayakumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can include scriptlet inside a <a> tag like this:

<a href="http://localhost:8080/lab5/newcategory.jsp?category= <%=rs.getString(1)%>" > rs.getString(1) </a>

Your expression scriplet has to be inside the double quotes.

This works perfectly!!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65105
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But violates best practices. Scriptlets should no longer be used in JSPs. The JSTL and EL are more appropriate choices.
 
Stefan Evans
Bartender
Posts: 1777
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And I guess while we're talking best practice, it would be worth noting that SQL code doesn't belong on a JSP page either.
You should never be dealing with a result set object in a JSP.
Write a bean method that runs the query and copies the result into a List of beans.
Your JSP page can then iterate over that list.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic