943,788 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Marked Solved
  • Views: 6129
  • JSP RSS
Jan 5th, 2009
0

Create Dynamic Hyperlink

Expand Post »
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:

JSP Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khalidmehmood is offline Offline
16 posts
since Jul 2007
Jan 5th, 2009
0

Re: Create Dynamic Hyperlink

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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is online now Online
6,656 posts
since Dec 2004
Jan 5th, 2009
0

Re: Create Dynamic Hyperlink

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:

JSP Syntax (Toggle Plain Text)
  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.
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jan 5th, 2009
0

Re: Create Dynamic Hyperlink

OK Thanks i will try this approach, but whats problem with this syntax?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khalidmehmood is offline Offline
16 posts
since Jul 2007
Jan 5th, 2009
0

Re: Create Dynamic Hyperlink

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>
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Jan 6th, 2009
0

Re: Create Dynamic Hyperlink

ok thanks a lot.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khalidmehmood is offline Offline
16 posts
since Jul 2007
Jan 9th, 2009
0

Re: Create Dynamic Hyperlink

> 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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Jan 19th, 2009
0

Re: Create Dynamic Hyperlink

JSP Syntax (Toggle Plain Text)
  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?
Reputation Points: 10
Solved Threads: 1
Light Poster
dreamer14 is offline Offline
31 posts
since Sep 2008
Jan 20th, 2009
0

Re: Create Dynamic Hyperlink

Yes exactly thanks a lot brother for your kind suggestion, GOD bless you...
best regards
Khalid Mehmood
Reputation Points: 10
Solved Threads: 0
Newbie Poster
khalidmehmood is offline Offline
16 posts
since Jul 2007
Jan 20th, 2009
0

Re: Create Dynamic Hyperlink

Nothing to bless. You dealing with database connection from view, JSP document, that is bad thing to do. Ever heard of MVC Model2?
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is online now Online
6,656 posts
since Dec 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: A Picture drop down list ?
Next Thread in JSP Forum Timeline: Problem migrating Tomcat 5.0 app to 5.5?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC