-1

I use the following code to set the src attribute in image tag from the servlet.but it's not working can any one help me to do this.

<img src="./imageDisplayProcess.do" alt="hai" width="117" height="160"/>

Servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        try{

                Class.forName("com.mysql.jdbc.Driver");
                Connection connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/raptor1_5","root","");
                Statement st1=connection.createStatement();
                ResultSet rs1 = st1.executeQuery("select Qimg from tbl_MatchImgToImg where C_code='R201AEV01'");
                String imgLen="";
                if(rs1.next()){
                    imgLen = rs1.getString(1);
                    System.out.println(imgLen.length());
                }   
                rs1 = st1.executeQuery("select Qimg from tbl_MatchImgToImg where C_code='R201AEV01'");
                if(rs1.next()){
                    int len = imgLen.length();
                    byte [] rb = new byte[len];
                    java.io.InputStream readImg = rs1.getBinaryStream(1);
                    int index=readImg.read(rb, 0, len); 
                    System.out.println("index"+index);
                    st1.close();
                    response.reset();
                    response.setContentType("image/jpg");
                    response.getOutputStream().write(rb,0,len);
                    response.getOutputStream().flush();             
                }



    }
        catch(Exception e){
            System.out.println(e);
        }
        }

web.xml

<servlet>
    <servlet-name>imageDisplayProcess</servlet-name>
    <servlet-class>get.imageDisplayProcess</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>imageDisplayProcess</servlet-name>
    <url-pattern>/imageDisplayProcess.do</url-pattern>
  </servlet-mapping>
KVK
  • 1,257
  • 2
  • 17
  • 48

1 Answers1

2

Assuming your servlet is bound to imageDisplayProcess.do:

The images you reference with -Elements in HTML are fetched using an HTTP GET request. What you implemented in the servlet is a POST request.

For GET requests you use the doGet() method instead of the doPost().

Matthias Wimmer
  • 3,789
  • 2
  • 22
  • 41
  • i chage the code from doPost to doGet method but no use – KVK Feb 23 '16 at 10:00
  • You should provide more context then. How have you set up the servlets and the routings? What is returned by the server when you send the request for the image? What is in the logfile of your servlet container? – Matthias Wimmer Feb 23 '16 at 10:06
  • After making some corrections it works fine I changed my xml and image tag from hai to hai – KVK Feb 23 '16 at 10:27