I have a problem. I'm trying to add pictures to a jsp file in my project but they are not displayed. The image is in BookStoreWebsite/web/images/BookstoreLogo.png.
Here is my jsp file index.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Evergreen Books - Online Books Store</title>
</head>
<body>
<div align="center">
<div>
<img src="images/BookstoreLogo.png" />
</div>
</div>
<div align="center">
<h3>This is the main content: New Books, Best Selling Books:</h3>
<h2>New Books:</h2>
<h2>Best-Selling Books:</h2>
<h2>Most-Favourite Books:</h2>
</div>
<jsp:directive.include file="footer.jsp" />
</body>
</html>
But, when i use images from the internet with a link, it works. Here is an example:
<div align="center">
<div>
<img src="https://image.flaticon.com/icons/png/512/3/3901.png" />
</div>
</div>
I tried various solutions but failed to fix it:
1)I tried this:
<img src="http://localhost:8080/BookStoreWebsite/web/images/BookstoreLogo.png" />
2) And this
<img src="<%= request.getContextPath() %>/images/BookstoreLogo.png">
3)I tried to put the picture in the directory of my project.
4) I made a project in Eclipse IDE with Tomcat 9.0. I thought maybe that was the issue. Then I created the same project in IntelliJ Idea with Glassfish. The same result. I suspect, perhaps the problem is with the servlet or with web.xml but i don't know for sure.
What am I missing here? Thanks!
Here is the servlet:
@WebServlet("/")
public class HomeServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HomeServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String homepage = "frontend/index.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(homepage);
dispatcher.forward(request, response);
}
}
Here is web.xml in Eclipse:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>BookStoreWebsite</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Here is web.xml in Intellij:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<description>JAX-WS endpoint</description>
<display-name>WSServlet</display-name>
<servlet-name>WSServlet</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>WSServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
</web-app>