Results 1 to 5 of 5
  1. #1
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default Regarding problem with displaying image in jsp

    Hi to all , i am facing with problem when i am displaying an image in jsp.
    My Code contains on jsp and one servlet as shown below.

    In my JSP

    Java Code:
      <a href = "displayImageServlet?id=12" >Display Image </a>
    Myservlet code contains

    Java Code:
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    			throws ServletException, IOException {
    
    		try {                               
            	Connection con = new ConnectionDb().getConnection();
                PreparedStatement ps = con.prepareStatement("select photo,contenttype,filename from photos where id = ?");
                String id = request.getParameter("employeeId");
                ps.setString(1,id);
                ResultSet rs = ps.executeQuery();
                rs.next();
                Blob  b = rs.getBlob("photo");
                String contentType = rs.getString(2);
                response.setContentType(contentType);
                response.setContentLength( (int) b.length());
                Map map = new HashMap();            
                map.put("image",b);
                map.put("text", "sandeep");
                request.setAttribute("map", map);
                RequestDispatcher rd =    request.getRequestDispatcher("/showimage.jsp");
                rd.forward(request, response);            
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    show image.jsp contains
    Java Code:
    <body>
    		<table>			
    			<tr>
    				<td>
    					<% Map map = (Map) request.getAttribute("map");
    			           Blob b = (Blob) map.get("image");
    					   String text = map.get("text").toString();
    					   InputStream is = b.getBinaryStream();
    					   OutputStream os = response.getOutputStream();
    					   byte buf[] = new byte[(int) b.length()];
    					   is.read(buf);
    					   os.write(buf);			
    					%>
    				</td>
    			</tr>
    			<tr>
    				<td>
    					${requestScope.map.text}
    				</td>
    			</tr>
    		</table>
    	</body>

    What my problem is image is displaying properly in showimage.jsp but i am unable display text in show.jsp

    Exceptions are

    IlleagalStateException ,
    Response is already committed.
    Last edited by sandeepsai39; 10-03-2010 at 09:02 AM.

  2. #2
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    I didin't get any replies am i posted in wrong thread,i.e instead of jsp and sevlets forums i posted it in New to java thread.am i right?

  3. #3
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default

    Some of us have this thing called a weekend...

    Normally an image servlet would simply send image data back to the client, not mess around with html. That is you should simply send (stream) the image through the response stream, if I remember correctly.

  4. #4
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    ok , sorry.

    Actually what i am thinking is am i asked my doubt in wrong way.

    Because java-forums is only forums i used for asking my questions?I get the answers from senior members ,what i am looking for very quickly .Thanks for java-forums and senior members and moderators who are helping us.

    coming to my problem ,is there is any other to resolve above type problem,is it possible to write simple text to jsp or html page using servlet output stream including with image

  5. #5
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default

    What you need is an image tag in your JSP page, which refers to another servlet that (like the current one) gets the image from the database and streams it to the client (in the response outputstream).

    So your first servlet simply gets the data (ie the text you want, and an id so you can get at the image).

    Look up image servlet. That should get you sometihng useful (especially if you can find BalusC's blog for it).

Similar Threads

  1. Displaying an image
    By SWEngineer in forum AWT / Swing
    Replies: 11
    Last Post: 01-28-2010, 06:48 AM
  2. displaying image into screen !
    By burningflower12 in forum AWT / Swing
    Replies: 14
    Last Post: 11-16-2009, 03:02 AM
  3. Replies: 0
    Last Post: 11-07-2009, 07:45 AM
  4. Displaying image in same jsp
    By SreenivasGurramkonda in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-08-2008, 07:01 AM
  5. A question about displaying an image...
    By SpaceY in forum New To Java
    Replies: 0
    Last Post: 08-24-2008, 07:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •