eddie skywalker
eddie skywalker

Reputation: 189

Append strings to our hyperlink in jsp

Can you give me any simple example?

For example I took an error when I try to add my strings:

String id = line.split(" / ")[0];
String ssid = line.split(" / ")[1]; 

table += "<tr><td><a href="person.jsp?id=<%=id%>&ssid=<%=ssid%>">Link</a></td></tr>";

Upvotes: 0

Views: 4122

Answers (2)

BalusC
BalusC

Reputation: 1109875

You're mixing JSP scriptlets in Java code and the " after href= is closing the string too early. This is incorrect. The basic fix would be:

table += "<tr><td><a href=\"person.jsp?id=" + id + "&ssid=" + ssid + "\">Link</a></td></tr>";

Note that you would also like to URLEncode those parameters to prevent that you end up with a malformed URL.

Better would be to stop using Java code and JSP scriptlets for presentation. You could use JSTL <c:url> and <c:param> to create links with URL-encoded parameters.

Upvotes: 1

d1e
d1e

Reputation: 6452

You are actually closing the string, by opening a href attribute, to insert a value. Escape ' " ' characters by using an escape character ' \ '.

table += "<tr><td><a href=\"person.jsp?id=<%=id%>&ssid=<%=ssid%>\">Link</a></td></tr>";

This will work.

Upvotes: 1

Related Questions