What would be the easiest way to work with a text file using JSP?

There is a live Java ServerPages (JSP) application in the * NIX box, which I administer somewhat with good permissions. The idea is to create a new but dead simple JSP page to manage some of the Korn Shell scripts I've been manipulating. So the goal is to create some kind of HTML form that will write some kind of scriptStatus.on / scriptStatus.off file:

#!usr/bin/ksh
# coolScript.sh
# This is my cool script that is being launched by cron every 10 minutes.

if [ -e scriptStatus.off ]
  then 
      # monitor disabled
  else
      # monitor enabled
fi

      

which can then be checked for existence during the script, so makes it easy to activate / deactivate it without actually having to deal with cron. Please let me know if this all makes sense and feel free to ask as many questions as possible.

Many thanks!

0


source to share


2 answers


You may have security issues. Consider what risks you have and take appropriate steps to authenticate users and make sure they are authorized for this operation. The steps required to do this depend somewhat on the servlet container you are using.

You don't need a library like Apache Commons IO for such a simple task. File.createNewFile and File.delete can be used if you are not worried about race conditions between two different users.



File flag = new File("/path/scriptStatus.off");
String message;
if (flag.delete())
  message = "Script enabled.";
else if (flag.createNewFile()) 
  message = "Script disabled.";
else
  /* Maybe missing directory, wrong permissions, race condition. */
  message = "Error: script state unknown.";

      

The cron task can check if the file (empty) exists or not and act accordingly.

+2


source


+1


source







All Articles