简体   繁体   中英

How to refresh static resources in Java Web Application?

I am developing an application that stores data dynamically and displays it as it is generated or uploaded. But the problem is when I am uploading any image then it should be displayed. It's path is also defined perfectly but until I refresh the whole application in eclipse it remains unchanged. So to update application, I have to refresh it.

My application is Java based and is useing JSP and Servlet. Is there any code that can be used to update or refresh the application?

Right now I am doing it by right click -> refresh or directly F5 it.

Or suppose I want to create directory then where will I have to create it that will be accesses easily without refresh.

When I'll host my application then which place is better for to store data? I know it's outside the web app but any specific place? Because there is no drive so how can I create or make directory and access that one in my code.

A solution to this is a design pattern called Observer . You should read about it.

Is there any code that can be used to update or refresh the application? Right now I am doing it by right click -> refresh or directly F5 it.

I think you need to look into the workspace re-fresh option in Eclipse ( was made available from Eclipse version 3.7.x ). You can enable it in Preferences > General > Workspace and select Refresh on access or as per need you can change to whatever setting you require.

在此处输入图片说明

When I ll host my application then which place is better for to store data ? I know it's outside the web app but any specific place ? Because there is no drive so how can I create or make directory and access that one in my code.

The location of static content is best if placed outside of the scope of the project itself. Maybe some other directory on your server ( be it any directory ).

I can share what I have normally seen as a trend in different applications that I have personally worked on. We had a separate tomcat server that just hosted all static content ( all media ) and our web application accessed that static-server (as it was named) within a secured network. All hits to static content could only be made via our application server and thus all direct hits were either rejected or not entertained at all.

Edit

I would suggest using a an absolute path and on windows environment you will HAVE to use the drive lletter and specify the path as X:\\some\\path if you want to hide your letter drive due to obvious security reason I can suggest another idea

Idea 1 : Make a separate drive (lets call it drive F) and make a folder with the name of "static". Then in your application , you just forward all requests to file uploads using this path ( F:\\static......) . I would advise loading the directory name from a property file instead of hard-coding it in your code

Idea 2 : If you cant make a separate drive, then make a directory namely "static" on the root of same drive ( C:\\static ) . Make a user group and give him read/write permissions on this drive and revoke writing permissions from this user on all other drives ( just in case someone messes up with this user-group). Next do the same thing as above ie specify this path into your application. One thing is that you would have to run your application with that specific user-group to ensure that the permissions security you have setup can be implemented.

Best practice is to store such data outside the webapp's tree to avoid issues on redeployment.

You can't serve these files directly though, commonly it's done by creating a servlet mapped to eg. images/* ; that parse the request URL (eg ) and based on that url fetches and serves. You need to set the correct MIME type for the output, and streamcopy the requested file to the servlet's output.

A well explained example of such a servlet can be found on BalusC's blog .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM