Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
mike zhang
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • 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
  • 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 ?
 
Hari babu
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • 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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • 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
 
Steve Yu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • 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 ?
 
Elinor Chang
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • 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.
 
Tim Holloway
Bartender
Posts: 18709
71
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Dana Hanna
Ranch Hand
Posts: 227
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On another note, you could use a database to store the info... It seems more appropriate...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!