Create Dynamic Hyperlink

Thread Solved

Join Date: Jul 2007
Posts: 16
Reputation: khalidmehmood is an unknown quantity at this point 
Solved Threads: 0
khalidmehmood khalidmehmood is offline Offline
Newbie Poster

Create Dynamic Hyperlink

 
0
  #1
Jan 5th, 2009
Hi Experts!
I am trying to build dynamic hyperlinks but i cant do this. i am new to JSP. please guide me.
here is my code:

  1. <%
  2. try{
  3. Class.forName("com.mysql.jdbc.Driver").newInstance();
  4. connection = DriverManager.getConnection(connectionURL, "root", "root");
  5. statement = connection.createStatement();
  6. rs = statement.executeQuery("SELECT * FROM contacts");
  7. while (rs.next()) { %>
  8. <a href="viewdata.jsp?value1="+<%rs.getString(1)%>+">"+<%out.println(rs.getString(1));%></a>
  9.  
  10. <%}
  11. rs.close();
  12. }
  13. catch(Exception e)
  14. {
  15. out.println(e);
  16. }
  17.  
  18. %>

how can i build the dynamic hyperlink with out.println inside JSP <%%> tag

regards
Khalid Mehmood
Last edited by peter_budo; Jan 5th, 2009 at 5:46 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,197
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Create Dynamic Hyperlink

 
0
  #2
Jan 5th, 2009
Data retrieved from database with use of servlet (not like your attempt above), returned back or passed to next page where with use of JSTL you would be able to do it much simpler.
You can pick up some general hints and tips from this tutorial
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Create Dynamic Hyperlink

 
0
  #3
Jan 5th, 2009
Also when you write something outside the <% %> it is displayed the way you write (HTML style).

So when you write this:

<a href="viewdata.jsp?value1="+sth+">"+ . . .

It will not concat. It is not java and it is not a String to concat it.
If you want to display the value of a variable do this:
<%= rs.getString(1)%>
It will put whatever value it has as "part" of the HTML page.
So might want to write this:

  1. <a href="viewdata.jsp?value1=<%= rs.getString(1)%>"><%=rs.getString(1)%></a>

No semicolon inside the <%= %> and notice the: "" of the href, as well as where the <> opens and closes:

<a href= "viewdata.jsp?value1=<%= rs.getString(1)%>" >
<%=rs.getString(1)%>
</a>


Also make sure to read peter_budo's post and follow his advice. My post was just for informative purposes. It is not correct to open database connections inside a jsp, and use the ResultSet that way to create a link
Last edited by javaAddict; Jan 5th, 2009 at 6:57 am.
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 16
Reputation: khalidmehmood is an unknown quantity at this point 
Solved Threads: 0
khalidmehmood khalidmehmood is offline Offline
Newbie Poster

Re: Create Dynamic Hyperlink

 
0
  #4
Jan 5th, 2009
OK Thanks i will try this approach, but whats problem with this syntax?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,677
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 226
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Create Dynamic Hyperlink

 
0
  #5
Jan 5th, 2009
One more thing that I forgot to add in the example.
If you want the HTML to create a link like this:

<a href="viewdata.jsp?value1=John">John</a>

The jsp should be:

<a href="viewdata.jsp?value1=<%= rs.getString(1)%>"><%= rs.getString(1)%></a>
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 16
Reputation: khalidmehmood is an unknown quantity at this point 
Solved Threads: 0
khalidmehmood khalidmehmood is offline Offline
Newbie Poster

Re: Create Dynamic Hyperlink

 
0
  #6
Jan 6th, 2009
ok thanks a lot.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,619
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Create Dynamic Hyperlink

 
0
  #7
Jan 9th, 2009
> but whats problem with this syntax?

The changes to internal implementation of the model [business logic + data] which affect the view which shouldn't be the case. Also a design which employs separation of concern is far more easier to maintain, change and troubleshoot.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 21
Reputation: dreamer14 is an unknown quantity at this point 
Solved Threads: 1
dreamer14 dreamer14 is offline Offline
Newbie Poster

Re: Create Dynamic Hyperlink

 
0
  #8
Jan 19th, 2009
  1. while (rs.next()) {
  2. String abc=rs.getString("YourVariableInContactDatabase");
  3. out.print("<a href=\"viewdata.jsp?value1="+abc+"\">"+abc+"</a>");
  4. }
Is this wat u mean?
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 16
Reputation: khalidmehmood is an unknown quantity at this point 
Solved Threads: 0
khalidmehmood khalidmehmood is offline Offline
Newbie Poster

Re: Create Dynamic Hyperlink

 
0
  #9
Jan 20th, 2009
Yes exactly thanks a lot brother for your kind suggestion, GOD bless you...
best regards
Khalid Mehmood
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,197
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 485
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Create Dynamic Hyperlink

 
0
  #10
Jan 20th, 2009
Nothing to bless. You dealing with database connection from view, JSP document, that is bad thing to do. Ever heard of MVC Model2?
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC