Tal Tal - 2 years ago 186
Java Question

java web server - newly created file not found by browser, until server restart

I have a tomcat 7.0 web server on my local machine, when doing a GET call to server i create a file (image), save it on server and give the browser the URL to fetch.

The browser gets a 404 "file not found" for the URL, even after I refresh the browser same 404.
Only after I restart the server the browser finds the file and all goes as planned.

I have tried giving it read permissions, but it did not help.

Where to look for the problem?

Tomcat Version : Apache Tomcat/7.0.12,
Servlet Specification Version : 3.0.

the file is saved under /projectName/WebContent/resources/images.

URL for accessing file: "http://localhost:8080/projectName/resources/images/2.png."

before a response to the GET call is returned the file already exists (also the file is not found after browser refresh - meaning another call).

as for cache settings, I have none, here is my web.xml

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>ProjectName</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/configuration/dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/configuration/applicationContext.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

Answer Source

If this is part of your application path, I presume identified in a context.xml, then it will not be visible until the context is redeployed. When a context is deployed, it is copied to work and run from there. Only WEB-INF/web.xml is watched for changes, changes to other files should not have any effect (except for JSP if you are running in dev mode).

  1. You can store files outside any webapp folder path, this is a best practice
  2. Write a servlet to fetch them from that directory for example /getpic/filename.png would look in c:\images\filename.png for the file, and stream it back to the user.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download