K.mady
K.mady

Reputation: 13

How to store and display image from Database using Servlet and EJB?

Can't find out how to add a profile picture to client when regstration in data base in web application using JSP, Registration Servlet and Entity Manager JPA for Mysql. For now I did this:

Registration Servlet

        final String username = request.getParameter("username");
    final String pass = request.getParameter("password");
    final String email = request.getParameter("email");
    String message ="";

    Part filePart = request.getPart("photo"); // Retrieves <input type="file" name="file">
    String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
    InputStream fileContent = filePart.getInputStream();

User Entity

    @Id
@Column(nullable = false)
private String username;

@Column(nullable = false)
private String password;


@Column(nullable = false)
private String email;

@Lob
@Column(name="PROFILE_PIC")
private byte[] image;

But I can't figure out how to correctly add the User in database which his profile picture to display it after when he logs in. The add without image in my UserDAO works fine but I don't know how to deal with images? MY DAO

    @Override
public void add(String username,String password, String email, byte[] image) {
    User user = new User(username,password,email);
    //em.createQuery( "INSERT INTO user (username, email,password, image) VALUES (?, ?, ?)");
    em.persist(user);

Upvotes: 0

Views: 110

Answers (0)

Related Questions