• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

In servlet, is it safe to open a same file on the server ?

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
In my servlet, it opens a file located on the server and gets its content (a special HTML string), then it sets this content as an attribute and forward to a JSP. In the JSP, it gets this content string and display it.
I know there are MANY ways to accomplish the same purpose. but, assuming I just want to open a file on the server and then read its content(String) and set in an attribute to JSP, the question is --- Since there will be MANY requests coming in, so is it safe to let the servlet open and read the file and then close it ? will there be thread issue ? Be reminded, I ONLY read that file, I don't write or modify it.
Thanks,
mike
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does the file's contents ever change (because of some other program's action) while your servlet is running?
If not, you might consider writing a small Listener that reads the file and stores its contents in a ServletContext property. That way, you'll only have to read the file once, and you'll safely do so before the servlet is ever invoked.
 
mike zhang
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ron Newman:
Does the file's contents ever change (because of some other program's action) while your servlet is running?
If not, you might consider writing a small Listener that reads the file and stores its contents in a ServletContext property. That way, you'll only have to read the file once, and you'll safely do so before the servlet is ever invoked.


well, the problem is I may have thousand files and their contents are fixed.
However, the problem is "which" file will be read is run time dependent. It depends on what user picks from some GUI, for example if user chooses "Europe"/"London"/"stock"/"report1", then it will read a specified file that matches to this criteria (the files are created in advance). So it is hard load so many files.
Is there any other way to ensure the the thread safe ?
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
You need to ensure thread safety while writing to a file so better way is to implement the SingleThredaModel intreface which ensures thread safety
Hari
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't use SingleThreadModel - that has nothing to do with this problem.
As long as nothing is writing to that file there is no reason not to just read it. The operating system knows the difference between opening a file to read it and opening it for both read and write. If lots of requests are using it you will probably be reading from the file system buffer anyway.
Bill
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, what I have thinking is -- instead of let servlet open and read the file and then set the content string in an attribute, can I just pass the file name to the destination JSP from servlet ? Then in the JSP I just do <jsp:include file=....>. This way it should not have any thread safety problem, right ?
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I believe you can certainly dynamically include JSPs. It is implemented in JSP as the directive <jsp:includ file.. />. If so, you do not need to worry opening/closing files yourself. Since you are only 'reading' files, there is no thread safety/data concurrency issue involeve.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSP includes are about as safe as you can get. It's pretty much mandated that for successful J2EE operation that multiple JSP threads should be able to include the same file.
Different OS's have different file locking mechanisms and different ways of handling changes (or even creation/deletion) on files. But for read-only access, you shouldn't have any problems.
 
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On another note, you could use a database to store the info... It seems more appropriate...
 
Wanna see my flashlight? How about this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic