0

I am developing JAVA web project by using netbeans IDE, I want to display some images from a folder outside the project's directory, after uploading them by my servlets, here is my code:

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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_3_1.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>views/index.jsp</welcome-file>
    </welcome-file-list>

    <servlet>
        <servlet-name>login</servlet-name>
        <servlet-class>controllers.LoginController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>login</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>index</servlet-name>
        <jsp-file>/views/index.jsp</jsp-file>
    </servlet>

    <servlet-mapping>
        <servlet-name>index</servlet-name>
        <url-pattern>/index</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>students</servlet-name>
        <servlet-class>controllers.StudentController</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>students</servlet-name>
        <url-pattern>/students</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>books</servlet-name>
        <servlet-class>controllers.BookController</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>books</servlet-name>
        <url-pattern>/books</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>admins</servlet-name>
        <servlet-class>controllers.AdminController</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>admins</servlet-name>
        <url-pattern>/admins</url-pattern>
    </servlet-mapping>

    <context-param>
            <param-name>uploads</param-name>
            <param-value>C:/Users/mohammad/Documents/NetBeansProjects/uploads</param-value>
    </context-param>

</web-app>

img in the jsp:

<img src="C:/Users/mohammad/Documents/NetBeansProjects/uploads/name.jpg"/>

servlet:

I am sending it inside a json object:

object.add("<img src=\""+getServletContext().getInitParameter("uploads")+"/"+book.getImage()+"\"/>");

I am sure that the image is exist in that folder, but it couldn't loaded, what is the problem here?

Mohammad
  • 3,449
  • 6
  • 48
  • 75

1 Answers1

0

You should add filter on web.XML file of image extensions that actually specify that what kind of external resources can be passed through front controller.. Will u please post your web.xml

U may try this link Upload an image to a Path set in web.xml using primefaces

Community
  • 1
  • 1
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
  • your adding a image file or only file name in your json ?? If you want to add image in json you need to first encode it in BASE64 String and then send it to the JSP and then decode and you may render it on JSP. But if Only you try to put the complete absolute path of image then it is ok.. I am confused that what kind of image info u r adding to the JSON(Either Location or File Itself). – Vikrant Kashyap Feb 19 '16 at 08:36
  • yeah I mean I am adding its location, not the file itself! – Mohammad Feb 19 '16 at 11:30