[英]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.
我已经可以显示公司表的信息(名称,地址和描述),但是我不知道如何加载保存在数据库中的图像并将其显示在jsp页面上。 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: DAO类:
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: 我的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: 我的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") 设置img标签src后,“ localhost:8080 / projectname /” +数据库已保存的网址(“ image / abc.jpg”)
private String archivoimg; 私有String archivoimg;
empre.setArchivoimg(localhost:8080/projectname/" + databse saved url ("image/abc.jpg")); empre.setArchivoimg(localhost:8080 / projectname /“ +数据库已保存的url(” image / abc.jpg“));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.