0

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">

4
Contributors
4
Replies
14
Views
6 Years
Discussion Span
Last Post by peter_budo
0

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?

Edited by javaAddict: n/a

-1
<%@ 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);
  }

%>

Edited by ~s.o.s~: Added code tags, learn to use them.

0

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!

Edited by mike_2000_17: Fixed formatting

1

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

Edited by peter_budo: n/a

Votes + Comments
You came to the rescue
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.