简体   繁体   中英

How to load and display a image from a database mysql in a jsp page?

I need to show the information of the companies in my database with their logotype. I could already show the information (name, address and description) of my company table, but I do not know how to load the image saved in my database and show it on my jsp page. This is my progress.

companies class

public class empresa {
    private int idEmpresa;
    private int fkpersona;    
    private String nombreE;
    private String direccion;
    private String rfc;
    private String texto;
    private InputStream archivoimg;
    private byte[] archivoimg2;}

I do not put all the code of this class because it is very extensive and I only put the attributes of my class

DAO class:

public List<empresa> MostrarEmpresa(){
        try{
        String sql="Select * From empresa Limit 6";
        PreparedStatement ps = conn.getConnection().prepareStatement(sql);
        ResultSet rs = ps.executeQuery();
        List <empresa> lista = new LinkedList<>();
        empresa empre;
        while(rs.next()){
        empre = new empresa();
        empre.setIdEmpresa(rs.getInt("idempresa"));
        empre.setNombreE(rs.getString("nom_empresa"));
        empre.setDireccion(rs.getString("direccion"));
        empre.setRfc(rs.getString("rfc"));
        empre.setTexto(rs.getString("descrip"));
        lista.add(empre);
        }
        return lista;
        }catch (SQLException e){
            System.out.println(e);
            return null;
        }
    }

my servlet:

public class InicioController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher rd;
        conexion conn = new conexion();
        EmpresaDAO fun = new EmpresaDAO(conn);
        List<empresa> lista = new LinkedList<>();
        lista=fun.MostrarEmpresa("");
        conn.desconectar();
        request.setAttribute("empresas", lista);
        rd = request.getRequestDispatcher("/index.jsp");
        rd.forward(request, response);
    }
}

my jsp:

<%@taglib  uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="es">
<head></head>
<body>
<c:forEach items="${empresas}" var="empresa">
                    <div class="row">
                        <div class="col-md-4 col-sm-6 portfolio-item">
                            <a href="#portfolioModal${empresa.getIdEmpresa()}" class="portfolio-link" data-toggle="modal">
                                <div class="portfolio-hover">
                                    <div class="portfolio-hover-content">
                                        <i class="fa fa-plus fa-3x"></i>
                                    </div>
                                </div>
       <img src="this is where I want to upload the image" class="img-responsive" alt="">
                            </a>
                            <div class="portfolio-caption">
                                <h4>${empresa.getNombreE()}</h4>
                                <p class="text-muted">${empresa.getDireccion()}</p>
                            </div>
                        </div>

                    </c:forEach>
</body>

create image folder in side the Web Page folder and write the image and it file file project path seved by Database

after set img tag src"localhost:8080/projectname/" + databse saved url ("image/abc.jpg")

private String archivoimg;

empre.setArchivoimg(localhost:8080/projectname/" + databse saved url ("image/abc.jpg"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM