Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

displaying photo in jsp  RSS feed

 
Rene Ribeiro
Ranch Hand
Posts: 35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I'm trying to display image in the JSP, but appears binary code. What is missing?


My Servlet


My Form



My Class

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two things:
  • The URL in the image tag is page-relative. It should be server-relative. See the JspFaq for more info.
  • Your servlet does not appeat to set content headers such as content-type and content-disposition.
  •  
    Rene Ribeiro
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Then it is not possible to make a virtual store using the BLOB data type, because we always have to run a servlet. I wanted when I open the browser already carry the image.

    The ideal would be to convert this content that displays in binary image to the browser. But this is not possible.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rene Ribeiro wrote:Then it is not possible to make a virtual store using the BLOB data type, because we always have to run a servlet. I wanted when I open the browser already carry the image.

    The ideal would be to convert this content that displays in binary image to the browser. But this is not possible.


    I don't know what you mean by all that. It most certainly is possible to use a servlet to send a binary image to the HTML page (remember, JSP creates an HTML page). You just need to make sure that you do it right.

    Have you fixed the problems I pointed out?
     
    Rene Ribeiro
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Got it!


    In my JSP
    [code] <c: forEach var = "prod" items = "$ {prod.listProd ()}"> <tr> <td> <img src = "servletlistaimagem" /> </ img> </ td> </ tr> </ c: forEach>
    [/ code]
    I just wanted to display the image, as if I put a value of type string displays normally. I could not understand how to convert the binary variable for photo.
     
    Rene Ribeiro
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My Browser

    ImageIO.write(???JFIF???Photosho....
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You don't. Passing the binary data to the JSP makes no sense. The JSP, which becames a normal HTML file when it is sent to the browser, uses an img tag that references a servlet that serves the binary data along with the appropriate headers. At no time is the JSP aware of the binary data.
     
    Rene Ribeiro
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    When you open the page should already appear the images. But how can I run my servet if not clicked on anything?
    By <jspbean> in jsp, as you would to convert this binary.?
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rene Ribeiro wrote:When you open the page should already appear the images.

    Yes, just like a normal HTML page. Are you forgetting that JSP just produces HTML? Your JSP page acts just like any other HTML page because that's all that is sent to the browser. So an img tag in a JSP acts exactly like an img tag in HTML.

    But how can I run my servet if not clicked on anything?

    By using a reference to it as the src url of the img tag. The browser will call it automatically just like any other image reference. The browser doesn't care if the source of the image is a servlet, an image file, a python script, or anything else. It just makes a request to the url and expects to get back an image response. The fact that it is generated by a servlet is irrelevant to the browser.

    By <jspbean> in jsp, as you would to convert this binary.?

    No. See above. This has nothing to do with JSP.
     
    Rene Ribeiro
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    So my question would be.
    How do I get this query and view the browser? If the value is to display an image (blob).


    public List<Prod> listProd() {

    ResultSet rs = null;
    Conect objConect = new Conect();

    try {

    sql = "select * from produt";
    objConect.openConMySQl();
    PreparedStatement stm = objConecta.con.prepareStatement(sql);
    objConecta.rs = stm.executeQuery();

    while (objConect.rs.next()) {

    Prod objProd = new Prod();

    objProd.setImage(objConect.rs.getString("image"));

    listProd.add(objProd);
    }

    } catch (SQLException e) {
    e.printStackTrace();
    }
    return listProd;

    }
     
    Stefan Evans
    Bartender
    Posts: 1834
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is a missing link here.

    In your JSP you iterate through your list of products, creating one image tag for each. But each image tag uses the same url.

    In your Servlet code you ALSO get your list of products, but you don't appear to do anything with it.
    The image you send back from the servlet isn't from your list of products, it is from a new blank object you have created in the servlet.


    What you have to realise:
    Each image tag needs a unique URL to make a request to load the image for that product.
    Thus far all of the images have the same source url. How are you going to tell the server which one you want?

    Your servlet should then look at this request, and provides the appropriate image from the list.

    That is the standard pattern as I understand it anyway.


     
    Stefan Evans
    Bartender
    Posts: 1834
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I merged your stuff with the following thread. I hope that is okay by you.
     
    Rene Ribeiro
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi!

    It can obtain a result of this consultation directly to your browser, without using servlet?

    How would it be?

     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rene, are you reading my replies? Do you understand that JSP is of no use to you and that you need to use img tags?
     
    Rene Ribeiro
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi!

    I understand the following:
    When I open the jsp page, I am displaying standard HTML and can use scriplets or import Java classes.
    And servlets are used to interact JSP and Java classes. Would not it?
    And casso the servlets run from a JSP action, but will run as an image and I'm not making any action?
    I'm not entedendo it.
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rene Ribeiro wrote:
    When I open the jsp page, I am displaying standard HTML and can use scriplets or import Java classes.

    Actually, not really. The use of Java and scriptlets in JSP has been obsolete since 2002 when JSP 2 was introduced. JSP should be written using the JSTL and EL, not Java code.

    And servlets are used to interact JSP and Java classes. Would not it?

    This is true.

    And casso the servlets run from a JSP action, but will run as an image and I'm not making any action?

    I don't know the word "casso".

    What it comes down to, is that you want to display an image in an HTML page (the fact that the HTML is generated from a JSP is irrelevant). You do that with an img tag.

    The src attribute of the image tag identifies the URL from which the image data will be served as the response. Whether this URL identifies an image file or a servlet that responds with the image data, the image will be displayed.

    The important point is that the image data is not read or used in any way from the request that creates the HTML page from the JSP, it is served as the response from the servlet in a separate request initiated by the img tag in the HTML page.
     
    Rene Ribeiro
    Ranch Hand
    Posts: 35
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How do I show this? After running my servlet.

    as a result code below:
    com.mysql.jdbc.Blob@70165037



     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 65833
    134
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    First of all, it can't be both a JPG and a PNG. Which is it?

    Secondly, what's the point of trying to print out the writer object?

    You might want to take a look at this FAQ entry.
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!