| | |
Create Dynamic Hyperlink
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved
![]() |
•
•
Join Date: Jul 2007
Posts: 16
Reputation:
Solved Threads: 0
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:
how can i build the dynamic hyperlink with out.println inside JSP <%%> tag
regards
Khalid Mehmood
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)
<% 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
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.
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
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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:
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
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)
<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
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>
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
> 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.
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.
•
•
Join Date: Sep 2008
Posts: 21
Reputation:
Solved Threads: 1
JSP Syntax (Toggle Plain Text)
while (rs.next()) { String abc=rs.getString("YourVariableInContactDatabase"); out.print("<a href=\"viewdata.jsp?value1="+abc+"\">"+abc+"</a>"); }
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Simple ASP.Net Login Page using C# (C#)
- Cannot find server or DNS Error - please help! (Viruses, Spyware and other Nasties)
- How can i add textboxes dynamically in an asp page (ASP)
- Basic SQL made Hard (MS SQL)
- linking issues (PHP)
Other Threads in the JSP Forum
- Previous Thread: A Picture drop down list ?
- Next Thread: Problem migrating Tomcat 5.0 app to 5.5?
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish imagetodatabse imageupload internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial video web






