File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jsp and images

 
Sabarish Jaganathan
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to all i had been insert image name (with file format) in to database using jsp i need to retrive the name of the image in the hyperlink so if admin clicks on the link image will be displayed but i couldn't able display it with using <img> src tag how to display it please any body help me i will upload my code below


code for retriving image

<%@page import="java.sql.ResultSet"%>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>View Image Page</title>
</head>
<body>
<table width="100%" border="0">
<%!Statement st=null;
Connection con=null;
%>
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:vishvum");
st=con.createStatement();
ResultSet rs=null;
try
{
rs=st.executeQuery("select id from num");
while(rs.next())
{
%>
<table width="70%" height="160" border="1" align="center">
<tr>
<!-- Mention Directory where your images has been saved-->
<td> <img src = "C:/Users/JAGANATHAN/Documents/NetBeansProjects/visuvam project/UploadedFiles/<%=rs.getString("id") %>" width="115" height="128" <a href="retrive.jsp?Image=<%=rs.getString("id")%>"></a> </td>
</tr>
</table>
<%
}
}
catch(Exception e)
{
out.print(""+e.getMessage());
}
%>
</table>
</body>
</html>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64204
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some important questions you need to answer:
  • Why are you using a file path for the image? That will never work anywhere except for your own PC.
  • Why is there Java code in your JSP? That's an obsolete practice from over 12 years ago.
  • Is more than one person going to use this web app? Because the way the page is written, it will break if more than one person acesses it.

  • For the above:

    The path to the image must be a URL to resolves that the image file (or any resource that will serve the image data). It cannot be a file path.

    Any Java code needed to get data for the page must be in the page controller or classes that the controller calls. It should not be in the JSP. Ever. You should only be using the JSTL and EL on the JSP page.

    Even if you choose to use Java scriptlets on the page (and you should not) the use of scriptlet declarations makes the page non-thread-safe. Never use declarations on a JSP page. This problem solves itself if you remove all Java code from the JSP.
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic