File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes do anyone know how to create hyperlink by out.print command. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "do anyone know how to create hyperlink by out.print command." Watch "do anyone know how to create hyperlink by out.print command." New topic
Author

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

chenchih lee
Greenhorn

Joined: Feb 18, 2013
Posts: 8
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

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

Joined: Feb 18, 2013
Posts: 8
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

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

Joined: Feb 18, 2013
Posts: 8
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

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

Joined: Feb 18, 2013
Posts: 8
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

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

Joined: Feb 18, 2013
Posts: 8
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Here's the spoon:
chenchih lee
Greenhorn

Joined: Feb 18, 2013
Posts: 8
so is it like this
<a href="<%= rs.getString("link") %>">whatever</a>
chenchih lee
Greenhorn

Joined: Feb 18, 2013
Posts: 8
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

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

Joined: Feb 18, 2013
Posts: 8
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
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: do anyone know how to create hyperlink by out.print command.