Getting image from database using Ajax

I am trying to read an image from a database using Ajax, but I could not read xmlhttp.responseTextfor img src. The image is saved as binary data in the database, and is also extracted as binary data. I use Ajax in JSP because I want to give the user the ability to upload images, and I will look at the last loaded image, when I hover over the Ajax action it will be activated and return the image, the problem is reading img from the answer.

This is the Ajax function:

function ajaxFunction(path) {
    if (xmlhttp) { 
        var s = path;
        xmlhttp.open("GET", s, true); 
        xmlhttp.onreadystatechange = handleServerResponse;
        xmlhttp.send(null);
    }
}

function handleServerResponse() {
    if (xmlhttp.readyState == 4) {
        var Image = document.getElementById(Image_Element_Name);
        document.getElementById(Image_Element_Name).src = "data:" + xmlhttp.responseText;
    }
}

I also got an exception on the server:

10415315 [TP-Processor1] WARN core.MsgContext  - Error sending end packet
java.net.SocketException: Broken pipe
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
    at org.apache.jk.common.ChannelSocket.send(ChannelSocket.java:537)
    at org.apache.jk.common.JkInputStream.endMessage(JkInputStream.java:127)
    at org.apache.jk.core.MsgContext.action(MsgContext.java:302)
    at org.apache.coyote.Response.action(Response.java:183)
    at org.apache.coyote.Response.finish(Response.java:305)
    at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:281)
    at org.apache.catalina.connector.Response.finishResponse(Response.java:478)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:154)
    at org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:200)
    at org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:283)
    at org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:773)
    at org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:703)
    at org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:895)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:685)
    at java.lang.Thread.run(Thread.java:619)
10415316 [TP-Processor1] WARN common.ChannelSocket  - processCallbacks status 2
+3
source share
2 answers

? URL- .

, AJAX PHP, , URL- , - :

img = document.getElementById("myimage");
img.src = URL; // where URL will contain the URL you got from the AJAX call 
+2

, . , src <img> URL-. - .

JSP/Servlet URL Servlet, InputStream OutputStream Java IO .

Javascript:

function changeImage(newSrc) {
    document.getElementById(Image_Element_Name).src = newSrc;
}

, Servlet.


SocketException: broken pipe - , . Ajax , , .

0

Source: https://habr.com/ru/post/1745616/


All Articles