0

Everything is working well. When I delete Code for Retrieving image then I get user details in table and when I put the code to retrieve image along with the code of printing user details then I only get image as an output. I wanted to print both of them and inside the table as other details appears.

code is shown below:

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Welcome
 */
public class Welcome extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Welcome() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletOutputStream out =response.getOutputStream();
        response.setContentType("text/html");
        String uniroll =(String) request.getAttribute("UROLL");


        out.println("<html>");
        out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"Bootstrap/bootstrap.min.css\">");
        out.println(" <link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">");
        out.println("<body>");
        out.println("<div class='container'>");
        out.println("<h3>Welcome</h3> ");
        try{

            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/college_record","root","20wasadk");
            String query="SELECT * FROM students_detail WHERE University_Roll=?";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, uniroll);
            ResultSet rs =ps.executeQuery();

            while(rs.next()){
                String imgLen="";
                imgLen=rs.getString(11);
                int len=imgLen.length();

                byte[] rb =new byte[len];
                InputStream readImg =rs.getBinaryStream(11);
                int index=readImg.read(rb, 0, len);


                out.println("<section class='col-md-6' id=\"signupbox\" >");
                out.println("<table class='table table-hover'>");
                out.println("<tr><th>First Name</th><td>"+ rs.getString(1)+"</td></tr>");
                out.println("<tr><th>Last Name</th><td>"+ rs.getString(2)+"</td></tr>");
                out.println("<tr><th>Sex</th><td>"+ rs.getString(3)+"</td></tr>");
                out.println("<tr><th>Father's Name</th><td>"+ rs.getString(4)+"</td></tr>");
                out.println("<tr><th>Class Roll</th><td>"+ rs.getString(5)+"</td></tr>");
                out.println("<tr><th>University Roll</th><td>"+ rs.getString(6)+"</td></tr>");
                out.println("<tr><th>Branch</th><td>"+ rs.getString(7)+"</td></tr>");
                out.println("<tr><th>Contact No</th><td>"+ rs.getString(8)+"</td></tr>");
                out.println("<tr><th>Permanent Address</th><td>"+ rs.getString(9)+"</td></tr>");
                out.println("<tr><th>Password</th><td>"+ rs.getString(10)+"</td></tr>");
                out.println("<tr><th>Index</th><td>"+index +"</td></tr>");

                ps.close();
                response.reset();
                response.getOutputStream().write(rb,0,len);
                response.getOutputStream().flush();

            }           



            out.println("<table>");
        }

        catch(ClassNotFoundException ce){
        ce.printStackTrace();
        }
        catch(SQLException se){
        se.printStackTrace();
    }   
        out.println("<section >");
        out.println("<div>");
        out.println("</body>");
        out.println("</html>");

    }

}

1 Answers1

0

You could Base64-encode your image data and use the data URI in the src attribute of an img tag, e.g. <img src="data:image/png;base64,iVBORw0KGgoAAAANS... (the Base64 encoded image data) ...8bgAAAAASUVORK5CYII=”>.

Few notes:

  • It is not supported in old IE versions (older than IE8, and the size of data is limited in IE8);
  • I don't think it is a good idea to embed the dynamic images this way, proper way would be to have a separate servlet just to serve the images, and use a normal URI in the src attribute. See the Important Notes part in the above linked page.
Jozef Chocholacek
  • 2,874
  • 2
  • 20
  • 25