954,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Create Dynamic Hyperlink

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:

<%
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
statement = connection.createStatement();
rs = statement.executeQuery("SELECT * FROM contacts");
while (rs.next()) { %>
<a href="viewdata.jsp?value1="+<%rs.getString(1)%>+">"+<%out.println(rs.getString(1));%></a>

<%}
rs.close();
}
catch(Exception e)
{
out.println(e);
}

%>


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

regards
Khalid Mehmood

khalidmehmood
Newbie Poster
16 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

peter_budo
Code tags enforcer
Moderator
15,432 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 901
 
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

OK Thanks i will try this approach, but whats problem with this syntax?

khalidmehmood
Newbie Poster
16 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

One more thing that I forgot to add in the example.
If you want the HTML to create a link like this:

John

The jsp should be:

<%= rs.getString(1)%>

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

ok thanks a lot.

khalidmehmood
Newbie Poster
16 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

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

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 733
 
while (rs.next()) { 
String abc=rs.getString("YourVariableInContactDatabase");
out.print("<a href=\"viewdata.jsp?value1="+abc+"\">"+abc+"</a>");
}

Is this wat u mean?

dreamer14
Light Poster
31 posts since Sep 2008
Reputation Points: 10
Solved Threads: 1
 

Yes exactly thanks a lot brother for your kind suggestion, GOD bless you...
best regards
Khalid Mehmood

khalidmehmood
Newbie Poster
16 posts since Jul 2007
Reputation Points: 10
Solved Threads: 0
 

Nothing to bless. You dealing with database connection from view, JSP document, that is bad thing to do. Ever heard of MVC Model2?

peter_budo
Code tags enforcer
Moderator
15,432 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 901
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You