How To Insert Image To A JSP File
I have Tried Using The Flowing Code In This any Error Is There
Please Give The Answer
This Is My Code I Used
<img src="SOCIALWEBSITE/Image/login.jpg" width="250" height="250" alt="img">

Recommended Answers

All 4 Replies

What error do you get? You should also write like this:
<img src="SOCIALWEBSITE/Image/login.jpg" width="250" height="250" alt="This is an image" />

Don't forget the /> at the end.

The above will not solve your problem, though. You need to post what error do you get. Is the image file at the correct location?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %> 
<%@page import ="javax.servlet.*" %>
<%@page import= "javax.servlet.jsp.*" %>
<%@ page language="java" %>

<%
JspWriter wr = pageContext.getOut();
Connection con=null;
PreparedStatement pst=null;
try
{
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con = DriverManager.getConnection("jdbc:odbc:ruhi","system","system");
	 pst=con.prepareStatement("insert into repository values(?,?,?,?,?)");
	File file=new File("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Water lilies.jpg");
	FileInputStream fis=new FileInputStream(file);
	pst.setString(1,"Ship");
	pst.setString(2,"Detroit");
	pst.setString(3,"yes");
	pst.setString(4,"CompleteProduct");
	pst.setBinaryStream(5 ,fis, (int)fis.available());
     int i = pst.executeUpdate();
    if(i!=0){
      wr.write("image inserted successfully");
    }
    else{
      wr.write("problem in image insertion");
    }  
  }
  catch (Exception e){
    System.out.println(e);
  }

%>

start quote:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %> 
<%@page import ="javax.servlet.*" %>
<%@page import= "javax.servlet.jsp.*" %>
<%@ page language="java" %>

<%
JspWriter wr = pageContext.getOut();
Connection con=null;
PreparedStatement pst=null;
try
{
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    con = DriverManager.getConnection("jdbc:odbc:ruhi","system","system");
     pst=con.prepareStatement("insert into repository values(?,?,?,?,?)");
    File file=new File("C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Water lilies.jpg");
    FileInputStream fis=new FileInputStream(file);
    pst.setString(1,"Ship");
    pst.setString(2,"Detroit");
    pst.setString(3,"yes");
    pst.setString(4,"CompleteProduct");
    pst.setBinaryStream(5 ,fis, (int)fis.available());
     int i = pst.executeUpdate();
    if(i!=0){
      wr.write("image inserted successfully");
    }
    else{
      wr.write("problem in image insertion");
    }  
  }
  catch (Exception e){
    System.out.println(e);
  }

%> 

end quote.

That is not what GuruMS has asked. Don't post random code that doesn't solve the problem of the one that made the question.
Especially code with no code tags. And especially code that is wrong!

1. Database connectivity form page view (JSP) - bad, read JSP database connectivity according to Model View Controller (MVC) Model 2to get basic idea of better approach
2. Absolute file path - bad, tell your web application through web.xml or other configuration file, if using framework, where to find directory and just swap relative path to file
3. Absolute file path for Windows - very bad! There is only 10% if not less Java web applications running in Windows environment. 90% is run on various flavours of Unix

commented: You came to the rescue +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.