0

I wish that once a user logs in to the web app, their name and image appears. However the problem I am having is that while the name shows up correctly, the image appears as characters. I am using a jsp and the code is written in java. The name and image are saved in a MySQL database and the image is saved as a blob.

   <%
     try{
          String session_id =null;
    HttpSession session1=request.getSession(false); 
    if(session1!=null){  
    session_id=(String)session1.getAttribute("name");  

    }
         Class.forName("com.mysql.jdbc.Driver");
      Connection  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore", "root", "*");
      Statement stmt = con.createStatement();
      ResultSet rs = stmt.executeQuery("select image,name from register where uid='"+session_id+"'");
      rs.next();
      String image = rs.getString("image");
      String name = rs.getString("name");


        %>
       <h1>Welcome <%out.print(name);%></h1>
        <table style="width:100%">
            <tr>
            <th>Image</th>
        </tr>
        <tr>
            <%=image%> 
        </tr>
        </table>

   <%    

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



    %>
  • No, this is not the best way to display image. Create a servlet that writes the image bytes to the response output stream and call the servlet where you want to display the image. BTW, why the image is String? Shouldn't be something like binary data? – Evgeni Enchev Jan 23 '19 at 12:26
  • Also see https://stackoverflow.com/questions/8499633/how-to-display-base64-images-in-html for a way to display an image. – Adder Jan 23 '19 at 12:30

0 Answers0