Issue
My code for displaying multiple images and trying to fetch their id[currency.jsp]
<div class="container-1" style="background-color:white">
<div style="color:Red;font-size:1.1em;font-weight:bolder;">${err}</div>
<%
try
{
DBConnector obj2=new DBConnector();
obj2.createConn();
Statement st=null;
st=obj2.getStatement();
ResultSet rs=null;
String query="select itemid,itemname,itemprice from currency";
rs=st.executeQuery(query);
int i=0;
%>
<table>
<%
while(rs.next())
{
int price=Integer.parseInt(rs.getString("itemprice"));
int id=Integer.parseInt(rs.getString("itemid"));
String name=rs.getString("itemname");
if(i==0)
{
%>
<tr>
<%
}
%>
<td style="width:220px;height:250px">
<form id="formimgid" action="Getdetailinfo" method="post">
<input type="hidden" name="getimgid" value="<%=id%>">
<a href="#" onclick="document.getElementById('formimgid').submit()">
<div class="cont">
<img src="Getimage.jsp?id=<%=id%>" width="220" height="150">
<div><%=name%></div><br>
<div>Price = <%=price%></div>
</div>
</a>
</form>
</td>
<%
if(i==3)
{
i=0;
%>
</tr>
<%
}
else
{
i++;
}
}
%>
</table>
<%
rs.close();
st.close();
obj2.closeConn();
}
catch(SQLException e)
{
System.out.println(e);
}
%>
</div>
</div>
Code for Getimage.jsp
<%
try
{
DBConnector obj1=new DBConnector();
obj1.createConn();
Statement st=obj1.getStatement();
ResultSet rs=null;
int id=Integer.parseInt(request.getParameter("id"));
String filename="image"+id+".jpg";
String query="select img from currency where itemid='"+id+"'";
{
rs=st.executeQuery(query);
String imglen="";
if(rs.next())
{
imglen=rs.getString("img");
}
rs=st.executeQuery(query);
if(rs.next())
{
int len=imglen.length();
byte rb[]=new byte[len];
InputStream readimg=rs.getBinaryStream("img");
int index=readimg.read(rb,0, len);
rs.close();
st.close();
obj1.closeConn();
response.setContentType("image/jpg");
response.setHeader("Content-disposition","attachment; filename=" +filename);
response.getOutputStream().write(rb,0,len);
response.getOutputStream().flush();
}
}
}
catch(SQLException e)
{
System.out.println(e);
}
%>
i am able to fetch and display multiple images what i want now when i click on an image it will open an new page containing information regarding that image and i am getting no help from anywhere.The form tag I have used in currency.jsp is useless it is only storing the id for the first image only.here i am trying to fetch image id when user click on it and then use that id to fetch data from databse and dislpay it on another page. Any help or alternative appreciated Thanks..
I am using Mysql as databse and Jsp servlet for front end.
Solution
if i have understood correctly you want to pass the image id to the next page when a user clicks the image, name or price.
So in you currency.jsp page replace your td tag with this
<td style="width:220px;height:250px">
<a href="Getdetailinfo.jsp?id=<%=id%>">
<div class="cont">
<img src="Getimage.jsp?id=<%=id%>" width="220" height="150">
<div><%=name%></div><br>
<div>Price = <%=price%></div>
</div>
</a>
</td>
In my code I have assumed that the page that will have the image details is called Getdetailinfo.jsp if it is not then change it in the href attribute of the a tag.
In the Getdetailinfo.jsp page you can get the image id by using
<%
int image_id = Integer.parseInt(request.getParameter("id"));
out.println(image_id);
%>
I have no experience in jsp but i am assuming that is how you can get the id from the querystring as you have done so in the getimage.jsp page.
Answered By - Mohammad C
Answer Checked By - Senaida (JavaFixing Volunteer)