1

I fetched data(some strings) from the database and i covered each string in a link in A.jsp page . Now if clicked a link,then the string that covered by the link is displayed in B.jsp . Here i note that in database i stored an image as a string .So here string is nothing but an image.

<% ResultSet rs=st.executeQuery("Select name,image from base64image");
int ii=0;
while(rs.next()){
    if(ii==0)
    out.println("<tr>");
    ii=1;
  %>
   <td><a href="B.jsp"> <img src='<%=rs.getString(2)%>'  height='200px;' width='200px' />n</a></td> 
  <% 
  i++;
  if(i%3 ==0 ){
   out.println("</tr>");
   ii=0;
  }

   }
out.println("</tr> </table>");
}

2 Answers2

0

Ok, so looking at your code, your using the expression tags for outputting the string you need. This string is the image source path, and the image is then used as the click link for your B.jsp page.

I'm assuming this expression is java, since the variable your using has a object type, and respective "getString()" method. If this is the case your code doesn't show the above "rs" object to be in jsp <% %> tags. Any java you write in jsp files need to be in these tags.

<% ResultSet rs=st.executeQuery("Select name,image from base64image"); %>

This being said using java in scriptlets in this manner is generally considered bad practice, however, I could understand the want to clean up the jsp file, and minimize the amount of lines to accomplish the query. This being said, just for completion's sake of this answer, this is how you might accomplish the same thing using jstl tags:

//first include the sql tag library in your Jsp
<%@ taglib prefix = "sql" uri = "http://java.sun.com/jsp/jstl/sql" %>

<sql:query dataSource = "datasource/here" var = "result">
   //full query here
   SELECT * from Employees;
</sql:query>

//You can then use the result variable using standard jsp
//Here you can grab the row, or sift through multiple results, etc.
<c:forEach var = "row" items = "${result.rows}">
    <a href="B.jsp"> <img src='${row.columnName}'  height='200px;' width='200px' />n</a>
</c:forEach>

info and examples here

However this is a lot of code in your jsp that isn't even the best way of going about this. Because you're only expecting one row returned, you would be forced to handle the results as if it returned multiple rows. (which is great however, if you needed to put this information into a table format)

The other solution would be to use java servlets. I can't fully recommend this though, as I don't know the structure of your code. This could either be easy, or difficult to implement into your system. However this still doesn't beat the one-line approach.

If you were to accomplish this via a separate servlet class, you would essentially call the class via an ajax call in the jsp, or using other various methods mentioned in this answer.

I hope I helped you solve your issue, and help any other people who stumble upon this answer!

0

Pass an Id with anchor tag in A.jsp

'<a href="B.jsp?Id="<%rs.getString(1)%>"> <img src='c%></a>

Now in B.jsp retrives all the names 1st column value from the database.If it matches with this id's value,then show the image by using tag

<img src="<%=rs.getString(2)" />