Image storage in the FS, Web application development, java

I make a web application, store the name of the Product image file in the product database, and store the images themselves in the file system on the disk D://images/*.jpg. After reading all the products from the database, I want to display them on the page. I can't display these images in JSP, I don't know where to specify the path that should be set in the config of my application, please show me how to do this and how to display images on JSP.

I work in the IDEA development environment, the java programming language. Perhaps the path to the images should be specified in web.xml, but it is not displayed in jsp <img src="D://images/${product.img}">.

Author: Kromster, 2016-11-30

1 answers

For example, you can specify the path on the disk to the image directory in the web application context parameter.

Web.xml

<context-param>
    <description>Где лежат мои картинки</description>
    <param-name>my.package.PATH_TO_MY_IMAGES</param-name>
    <param-value>d:/images</param-value>
</context-param>

Then you create a servlet, as @cache wrote, but with minor changes

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String filename = request.getPathInfo();
    String pathToMyImages = request.getServletContext().getInitParameter("my.package.PATH_TO_MY_IMAGES");
    File file = new File(pathToMyImages, filename);
    ...

Digression from the topic of path configuration
request.getPathInfo() returns what is behind the * in the /images / * template, prefixed with the / sign and excluding the request parameters.
http://foo/images/img001.png?param=123 -> /img001.png

Return to the topic
In addition to the context parameter, you can use Application Environment Entries. Some people (javaee developer) believe that this is preferable to context parameters.

Web.xml

<env-entry>
    <description>Где лежат мои картинки</description>
    <env-entry-name>path_to_my_images</env-entry-name>
    <env-entry-value>d:/images</env-entry-value>
    <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

We use it in the program.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String filename = request.getPathInfo();
    Context initContext = new InitialContext();
    String pathToMyImages = (String) initContext.lookup("java:comp/env/path_to_my_images");
    File file = new File(pathToMyImages, filename);
    ...

Or you can embed it in the servlet field. If it works, it's really preferable.

@WebServlet("/images/*")
public class FileServlet extends HttpServlet {
    @Resource(lookup="path_to_my_images")
    private String pathToMyImages;
    ...

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String filename = request.getPathInfo();
        File file = new File(pathToMyImages, filename);
        ...

To not edit the web.xml, on the tomcat server, can be added to the server configuration ($CATALINA_BASE/conf/server.xml for tomcat 7) global environment entry

<Server>
    ...
    <GlobalNamingResources>
        ...
        <Environment name="path_to_my_images" type="java.lang.String" value="d:/images"/>
        ...
    </GlobalNamingResources>
    ...
</Server>

Embedding a resource in the program is the same as above. In web.xml you don't need to add anything.
This is not a tricky way to have your own folder with pictures on each server, without resorting to editing the web.xml

And of course, nothing prevents you from using some configuration library that can work in a web application. These are available.

Images are inserted like this <img src="${request.contextPath}/images/${product.img}"/>

 1
Author: Sergey, 2016-12-01 13:09:14