• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

do anyone know how to create hyperlink by out.print command.

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to create a hyperlink which connect to google.com, by using out.print command, because the link are from the databse.

dabase information:
tablename: link
no link
1 www.google.com

==========================
code:
.............................................
while (rs.next()) {

out.print(rs.getString("id"));
out.println("<a href=" + rs.getString("link")+">" +link+ "</a>");
..............................

=========================================
how to link to www.google.com when i click on the link page
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you use out.print to create HTML markup?

The whole point of a JSP is to avoid creating HTML markup in Java statements.

Why aren't you just creating the markup in template text?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. You do realize that putting Java code in a JSP is something that has been obsolete for 12 years, right?
 
chenchih lee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to create the markup in template text.

is there a better way rather than using out.print("<a href=.......
if there is can you tell me.

i want to input many link to the database, and the display the link with a hyperlink.
if there is many link, i can;t find a better way. if there is a better way please let me know.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HTML markup should simply be put into the JSP; Not inside Java code.

Do you not have any other HTML in your JSP?
 
chenchih lee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is t full source code:

<style type="text/css">
<!--
body,td,th {
color: #000000;
}

.style5 {color: #0000FF}
.style18 {font-size: 16px}
.style19 {font-size: 36px}
body {
background-color: #FFFFFF;
}

-->
</style>
</head>

<body>



<hr>
<form name="form1" method="post" action="">
<table width="744" border="1" align="center">
<tr>
<td width="734"><p>
<%
request.setCharacterEncoding("big5");
Connection con ;
PreparedStatement stmt = null;
ResultSet rs = null;
%>
</p>
<table width="475" border="1" align="center">
<tr>
<td width="51" bgcolor="#99FF00"> </td>
<td width="64" bgcolor="#99FF00">link</td>

</tr>
<%
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/webapp?user=root&useUnicode=true&characterEncoding=big5");
stmt=con.prepareStatement("select id, link from link");
rs = stmt.executeQuery();
while(rs.next()) {
out.print("<tr><td>");
out.print(rs.getString("id"));
out.print("</td><td>");
// out.print(rs.getString("link"));
// out.print("<a href = rs.getString("link")> rs.getString("link") </a>");
//String s=rs.getString("link");
out.println( "<a href =" + rs.getString("link") +"> " + rs.getString("link") + "</a>");
out.print("</td></tr>");

}
%>



</table>
<p>
<%
} catch (Exception ex) {
ex.printStackTrace();
} finally {
if (stmt!=null) stmt.close();
if (rs!=null) rs.close();
}
%>
</p>
<p>  </p></td>
</tr>
</table>
<p>
<% /*
String name=((String)session.getAttribute("uid")==null)?"":(String)session.getAttribute("uid");
if(name.equals("")){
{response.sendRedirect("admin.jsp");}
} */

%>
</p>
</form>
</div>
</body>
</html>

================output============================
link
1 www.hotmail.com
2 www.google.com
3 www.yahoo.com


i wish click on the hyperlink it is link to the page.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, so there's plenty of HTML in template text (outside of JSP scriptlets). Your link should be like those. Remember you can end os open your scriptlets at any point. (And remener that you shouldn't be using Java code at all in a JSP, that's a bad practice in the first place.)
 
chenchih lee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so how will i chnaged the code to link to the link.

out.println( "<a href =" + rs.getString("link") +"> " + rs.getString("link") + "</a>");

this will not let me connect to google or yahoo or other link.
this only connect internal link or .jsp link. i put many link in the database, and i wish this link will connect to the properly link when i show the data out.

is there a better way of writting it.

thnaks.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to put an <a> element in your HTML, just do that. Outside of the scriptlet, of course. Just like you did in that example for <table> elements and so on. It's just HTML.

And there aren't any restrictions on what the href attribute can link to because you're just generating HTML which is going to be in the browser. If you're having problems with non-working links, that would be because you aren't generating the right value for the link. To find out why, use the "View Source" feature in your browser to see what value you generated, and then fix your code to generate the correct value.
 
chenchih lee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have a jsp page for user to input link, so the link the user input into the datbase, and it will direct to a new page which show the link they input(which is the soruce code i put above) .
do all the link is automatic drag form the database. so i am unalbe to use <a href= xxxxx> xxxxx </a>/

i need to print hte data from databse and make a hyperlink. but the hyperlink will not connect to what the user type in the link.

forexample: user type www.google.com into database==> it must show www.google.com with a connection to www.google.com.

is there a better way.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's the spoon:
 
chenchih lee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so is it like this
<a href="<%= rs.getString("link") %>">whatever</a>
 
chenchih lee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried with <a href="<%=rs.getString("link")%>">link</a>
but it link wrong. it will not link external link such as google or yahoo
it will only link internal jsp web

 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without knowing what that gets resolved to, there's no way we can help. What's the HTML that is actually created and sent to the browser?
 
chenchih lee
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
example www.google.com

it will go to after i click on the link ==>localhost:8080/link/www.google.com

i just want whne i click on it, it will redirect to google website.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you need to fix your JSP code so that it outputs a URL which does link to Google's home page. The first step for doing that is to find out what that URL is. So... What URL do you type into your browser if you want to go to Google? It isn't that URL you posted, is it?

And if I recall right, the URLs are coming from a database. That would mean you'd have to fix the database so it contained usable URLs.
 
reply
    Bookmark Topic Watch Topic
  • New Topic