DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   JSP (http://www.daniweb.com/forums/forum24.html)
-   -   Create Dynamic Hyperlink (http://www.daniweb.com/forums/thread166153.html)

khalidmehmood Jan 5th, 2009 4:09 am
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

peter_budo Jan 5th, 2009 5:50 am
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

javaAddict Jan 5th, 2009 6:53 am
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:

<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

khalidmehmood Jan 5th, 2009 6:57 am
Re: Create Dynamic Hyperlink
 
OK Thanks i will try this approach, but whats problem with this syntax?

javaAddict Jan 5th, 2009 7:03 am
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>

khalidmehmood Jan 6th, 2009 12:48 am
Re: Create Dynamic Hyperlink
 
ok thanks a lot.

~s.o.s~ Jan 9th, 2009 11:07 am
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.

dreamer14 Jan 19th, 2009 2:09 pm
Re: Create Dynamic Hyperlink
 
while (rs.next()) { 
String abc=rs.getString("YourVariableInContactDatabase");
out.print("<a href=\"viewdata.jsp?value1="+abc+"\">"+abc+"</a>");
}
Is this wat u mean?

khalidmehmood Jan 20th, 2009 12:02 am
Re: Create Dynamic Hyperlink
 
Yes exactly thanks a lot brother for your kind suggestion, GOD bless you...
best regards
Khalid Mehmood

peter_budo Jan 20th, 2009 4:48 am
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?


All times are GMT -4. The time now is 2:43 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC