DESEI-SELB
asked on
where do we put the text files?
I have an application which has links to some text files. I am not sure where do put these text files. I am thinking if they will be included in the ear file, then the applicaion has to keep getting redoployed every time there is small change in the one of the text files?
thanks,
thanks,
Correct. You may want to deploy them separately and redeploy only text files in case of changes.
Don't put them in the ear file. Put them in a separate directory; or even put them in database.
Put them in an external folder, and keep their location path in the properties file for your application.
So that when you change the files or add more files you simply need to go to that location to read those files.
So that when you change the files or add more files you simply need to go to that location to read those files.
Outside webapp.
One way (there are other ways) is to do the following:
Assume a virtual directory /data/ with your text files:
In the WEB-INF/web.xml:
<servlet>
<servlet-name>dispatcher</ servlet-na me>
<servlet-class>org.springf ramework.w eb.servlet .Dispatche rServlet</ servlet-cl ass>
<load-on-startup>2</load-o n-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</ servlet-na me>
<url-pattern>/data/*</url-pattern>
<url-pattern>*.html</url-p attern>
<url-pattern>*.json</url-p attern>
...
And let the servlet stream it out:
Something like:
import org.apache.commons.fileupl oad.util.S treams;
import org.apache.commons.io.File Utils;
protected ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
String realPath = null;
String path = request.getRequestURI();
if (path.startsWith(fromPath) ) {
realPath = toPath + path.substring(fromPath.le ngth());
} else {
Logger.getLogger(StaticCon tentElsewh ereControl ler.class. getName()) .log(Level .SEVERE, "Unknown path: {0}", path);
realPath = request.getPathTranslated( );
}
for (Enumeration<String> en = request.getHeaderNames(); en.hasMoreElements(); ) {
String name = en.nextElement();
String value = request.getHeader(name);
LOGGER.log(Level.INFO, "Header {0}: {1}", new Object[]{name, value});
}
if (realPath != null) {
File sourceFile = new File(realPath);
InputStream in = FileUtils.openInputStream( sourceFile );
OutputStream out = response.getOutputStream() ;
Streams.copy(in, out, false);
}
return null;
}
Assume a virtual directory /data/ with your text files:
In the WEB-INF/web.xml:
<servlet>
<servlet-name>dispatcher</
<servlet-class>org.springf
<load-on-startup>2</load-o
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</
<url-pattern>/data/*</url-pattern>
<url-pattern>*.html</url-p
<url-pattern>*.json</url-p
...
And let the servlet stream it out:
Something like:
import org.apache.commons.fileupl
import org.apache.commons.io.File
protected ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {
String realPath = null;
String path = request.getRequestURI();
if (path.startsWith(fromPath)
realPath = toPath + path.substring(fromPath.le
} else {
Logger.getLogger(StaticCon
realPath = request.getPathTranslated(
}
for (Enumeration<String> en = request.getHeaderNames(); en.hasMoreElements(); ) {
String name = en.nextElement();
String value = request.getHeader(name);
LOGGER.log(Level.INFO, "Header {0}: {1}", new Object[]{name, value});
}
if (realPath != null) {
File sourceFile = new File(realPath);
InputStream in = FileUtils.openInputStream(
OutputStream out = response.getOutputStream()
Streams.copy(in, out, false);
}
return null;
}
ASKER
This is too complicated.
gurvinder372, you suggested to put the path in the property file. can you please explain more. where is the property file? sorry I am very new to this. thanks,
gurvinder372, you suggested to put the path in the property file. can you please explain more. where is the property file? sorry I am very new to this. thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.