javaweb code and data separation Reflections

Today saw a blog post. as follows:

Often develop java web applications to have some friends to packaged, maintenance experience, improve our maintenance software can generally be separated from the content of variable and constant, structural remodeling software to achieve, including the reconstruction of the code levels, including reconstruction of the application directory file, the following I will briefly talk about my first experience.


       Our system is a java web application, but all the class files in the system, jsp, js, css, image files, and system dynamics upload doc, xls, image, wmv and other documents in the same project directory. DETAILED directory structure is as follows:

Application configuration context root is / MyApp
MyApp / the WEB-INF /
MyApp / ADMIN /
MyApp / Images /
MyApp / style /
MyApp / Files /
MyApp / videos /
......

 

    1, there is a problem
       in fact, files, videos folder, as users upload new content released regularly updated changes in the operation of the system, while other parts are changed only when a change in the release version of the system requirements, these two kinds of change different, the former is the result of normal use of the system, which is the result of system changes, and the change does not change the principle of separate, we hope this class 2 folders stored separately.
       As we publish web application package (myapp.war) need to application context root directory of all the files in a folder myapp are included, it is necessary to frequently changing files and folders videos move out.

 

    2, to explore solutions
       that we know, servlet There are two ways of steering, one request.getRequestDispatcher (requestPath) .forward (request,

Response) , it does not change the url, jsp directly forwards the request to a page, is a resonse.sendRedirect (urlPath) , which revision url addresses, jump to another request. But both of them had not even access the directory where the servlet outside the web application context (myapp), and all the files in the folder myapp we need to be packaged into myapp.war, so there is a contradiction.
       Additional methods, as well as a document reading mode by using java.io.File flow, then use the response servlet objects are written out to the page transmission, such a method may have access to the web container (such as Tomcat) operation where user system's root directory (such as windows of D: / or the linux / usr *, etc.), so that we can put files and other folders to move beyond myapp. For example, when the tomcat application context root directory is / usr / tomcat / webapps /, we do not allow files myapp, but rather into the / usr / upload / files, so we packed myapp.war, just load a version change the latest version of the program, users upload files are still on the same files using file access time () to carry out. Such as:
       File F = new new File ( "/ usr / Upload / Files / 2010 /. 3/15 / 010001.doc")

Use FileInputStream to read the file, and then write the file using the response page flows in outstream.
       Among these problems is the read files take up the JVM heap space, if there is 500MB large file, then the general default JVM heap space is not enough, you need to set the parameters in the JVM startup time, making it large enough to hold the file , of course, have to read and write files using the JVM to schedule, obviously efficiency will be relatively low, much slower than direct read.

 

    3, improved method
       there is no way to make both files in a folder outside myapp, it does not have the File class to read the file data stream it? In linux you can use a shortcut to resolve the directory is actually soft connectivity files.
       As we all know, in linux files and folders is the same data entity, you can use the same method to do the connection, operatively connected to a file or directory in another place, to reach the role of direct operation of the file or directory .
       First, a soft connection is established manner:
       LN -s DestinationPath the sourcePath
the files in the folder myapp outside, and in a particular application in webapps, such as a flexible connection to establish a ROOT files folder, as:
       LN -s / usr / upload / usr /
tomcat / webapps / ROOT / upload and tomcat container set in a context ROOT attribute  allowLinking = "to true"  , to access
       this way, we can myapp application, by direct url call, or redirect the response of way, access to the files in the file, such as:
       HTTP: // localhost: 8080 / the Upload / files / 2010/3/15 / 010001.doc
       As a result, access to upload shortcut would be tantamount to visit the upload folder, neither external resources do not have permission to access restrictions, and no use of the File class to read and write data stream, there will be no restrictions heap space, we realized volatility separate data file, and no memory consumption. 500MB file read and write, the heap does not need to set the space 500 above, only 64 can be a default.
       In summary, using the operating system characteristics - soft connection, to achieve a data file from a separate application package, the operating system implements the directory file access jump by modifying the file node I, which belongs to the underlying operating system implemented, compared to the application layer using the JVM class File to read and write more than 10 times faster, the Runtime JVM saving space.

    4, sum up
       my web application in order to allow ease of maintenance, the release of the program code and user data uploaded files separate, after a test of thinking and servlet redirect method of test for the File class file stream method, for linux file test method soft connection, it is best decided to use the last, while addressing the problem of file maintenance and operational efficiency.


Thinking sections:

If it is distributed how to do? If the data and the program is not how to do the same server?

In fact, a web container has its own solution: a virtual directory. If the data is not on that server corresponding program, as long as the web container in the data point to the virtual directory on the line. The virtual directory to share.


Published 29 original articles · won praise 3 · views 20000 +

Guess you like

Origin blog.csdn.net/yangleiGJ/article/details/54913797