Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with running a batch file on click in a JSP application

Status
Not open for further replies.

Khold

MIS
Jun 22, 2008
25
GB
Hi all,

I have the following code in my JSP app:

<a href="runApp('file://capScrAndSend.bat');" class="mainbars"> CONTACT ADMIN </a>

The batch file is present in the root directory of the web application. It does two things:
1.) runs a java class which is present in a sub-directory of the web application
2.) calls upon a vbscript which is also in a sub-directory of the web application

The Javascript code for the function runApp(), present in the <head> tag of the HTML in the JSP page is the following:

<script type="text/javascript">
function runApp(which) {
WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run (which,1,false);
}
</script>


The page is in the same directory as the batch file i.e. the root directory of the web application.

I have tried running the batch file by passing a lot of kinds parameters to runApp(), including its absolute path, but I get the usual "requested resource not available" HTTP Status 404 error (represented below):

type Status report

message /GSDC5Pv3/runApp('file://capScrAndSend.bat');

description The requested resource (/GSDC5Pv3/runApp('file://capScrAndSend.bat');) is not available.


I would be really grateful if someone could help me run a batch file of this kind (with a relative path to the web application) in any possible form, as I really need this functionality to be implemented.

Thanks in advance. Please do reach me if any more information is needed.



 
Hi Diancecht,

Yeah, that's what my current app will do, although the functionality I want is to take a screenshot of the user's workstation and then fire up the default mail client (which is Outlook).

I can take a screenshot with Java (using the awt.Robot class) and fire up the default mail client with the screenshot as attachment with VBScript.

However, I now realize that it is no use running a Java class that takes the screeshot on the server side. Is there any solution that can enable me to take a screenshot of the client's workstation (either from the server side or through client side scripting in Javascript/VBScript)?

Thanks.
 
You can use an applet to run a class client side, but I'm not sure about the security implications for that.

Cheers,
Dian
 
Yep. I am going to explore that.

Basically I have to digitally sign the applet. But I've seen from other disucssions in forums that javascript has problems calling digitally signed applets (I need to run the applet on click, and I also need to call VBScript to send off a mail with the screenshot the applet takes as attachment).
 
I don't know the requirements for your application but don't you think you're making things complicated by mixing all that technologies?

Cheers,
Dian
 
I have little choice with regard to that because of the requirements :)

I have an idea how to make this work, provided I can call on a signed applet inside a javascript function.
 
I have an update concerning my attempt to implement the screenshot capturing functionality on the client side using a signed applet. Please do bear with me if I am committing any obvious mistakes in my solution, as I have barely scratched the surface of Applet based Java programming.

These were the steps I followed:
a.) I first converted the Java class which is supposed to take a screenshot of the client's workstation and save it as a JPG in a pre-determined location, into an applet by extending the Applet class

b.) Then, I packaged this class into a jar file and signed it with a standard keystore

c.) I put this jar into the 'lib' folder of the root directory of my web application, and added the applet using the <applet> tag on the JSP page where it was needed

d.) I then wrote a JScript file with a function to call a public method present this applet (takeScreenShot()) [inspired by THIS ARTICLE, and stored in in the 'js' folder of the root directory of the my web application (This file also contains code to call a VBScript function [present in a separate .vbs file of course] to send the screenshot taken as an attachment in an e-mail by firing up Outlook)

e.) I created a link which calls upon the JScript function to do the above. Of course, I added references to the JScript and VBScript files in my JSP page

So, after setting up all this, I tested my application and here are the results:
The applet got added successfully to the page, though I would prefer it to be invisible to the user (i.e. when the applet fired, it took up a blank area on the JSP page, which I would like to avoid since it has no GUI). The security certificate showed up as well, and I OKed it.

On clicking the link on the page, the JScript was fired, but IE returned an error on the page. The error was:
Error: Path does not exist. Make sure the path is correct.
Code: 4096

Now, the JScript does get fired and calls on the VBScript sucessfully because Outlook does open up, though the new e-mail message is not created. This is because the takeScreenShot() method of the applet apparently is not doing it's job, and the VBScript is not finding the screenshot where it's supposed to be.

I'd like to know whether I need to sign the applet or modify its code in a different way such that when the user OKs the certificate, it can have access to the local file system. I am leaving all the lifecycle methods in the applet blank. All it has currently is a takeScreenShot() method which will behave the same way as a normal Java class.

I'd also like to know if an Applet can be added and its methods can be accessed without actually making it take up space on the JSP page (After the security certificate is OKed and the applet control activated, it doesn't take up space anymore...but is there a way to avoid this as well?)

Thank you everyone.
 
Just some ideas:

- You can set the applet size so it will be invisible to the user
- Is the applet method actually called?
- Who is throwing that error?

Cheers,
Dian
 
-I have made the width and height 0. Yet, when the applet loads for the first time, it has to be clicked in order to activate it and it takes up an arbitrary amount of space until the time the user clicks on it. Is there a way to avoid that?

-The applet method is not doing its job because the screenshot does not get created where it is supposed to on the client workstation. I will add a message box code to see if it enters the method at all and get back here on this

-The error is being thrown by IE i.e. the status bar shows the error on page icon and when I double click on it and click "show details", it highlights the error I presented above in my previous post. I am guessing the error is thrown by the VBScript, because it is the only other place where I mention the screenshot's path (it is a hardcoded location). The VBScript is invoked in turn by a JavaScript function.
 
Addition to above: I am sure the VBScript gets invoked by the Javascript because Outlook does fire up, but without a new message being created (possibly because the .AddAttachment which has the hardcoded path to the screenshot does not exist as the applet is not doing its job).
 
This article shows you how to avoid the activation part.

To check if the method is called, a System.out would show up in the Java console.

Cheers,
Dian
 
I will try that out, thanks.

I really an anxious to get the screenshot functionality working. I will report my observation to you when I get back to office tomorrow.
 
Allright...so I have a log of the stack in the Java Console.

It shows that the takeScreenShot() method of the java applet is being invoked, but access is denied.

Here is the stacktrace:
Code:
 liveconnect: Invoking method: public void RunScrCap.takeScreenShot()
java.security.AccessControlException: access denied (java.awt.AWTPermission createRobot)
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at java.awt.Robot.checkRobotAllowed(Unknown Source)
	at java.awt.Robot.init(Unknown Source)
	at java.awt.Robot.<init>(Unknown Source)
	at RunScrCap.takeScreenShot(RunScrCap.java:45)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.plugin.javascript.invoke.JSInvoke.invoke(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)
	at sun.plugin.com.MethodDispatcher.invoke(Unknown Source)
	at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)
	at sun.plugin.com.DispatchImpl$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.plugin.com.DispatchImpl.invoke(Unknown Source)

Now I guess the question is how do I modify my applet/sign my applet such that it allows access to the user's file system.
 
Addition to above: NOTE - Again, I would like to create a screenshot and save it to a predetermined location on the user's workstation and then attach this to a mail message.
 
I take the applet is already signed, so the problem may be related to the permissions given in the client machine to the vitual machive via the java.policy file.

I'd modify it and grant all permissions to test it

Cheers,
Dian
 
Hey,

Good news! I managed to run the applet without having to modify the policy environment on the client side.

I signed it and ran the code inside it as privileged code.

Thanks for all your inputs. I will reach the forums again for further advice from professionals like yourself.
 
This thread is alive again.

And that's because I attempted to add additional functionality to the java applet. Currently, I am hardcoding the location where the screenshot is to be stored and then picking it up to attach to a mail message based on the same location.

There was a new requirement to throw up a save dialog within the applet after the screenshot is taken so that the user can choose where to save the file, and then attach the file to the mail message from there.

So I implemented the same using the Java Swing API. In the code, I throw a save dialog where the user specifies the name of the file.

I then try to return the absolute path of the file so that the mail message can be fired up with this file as attachment (this is implemented in a VBScript function, so the applet returns a value to the client-side Javascript,which passes it on to a VBScript function).

My java console shows the following stack trace when I fire this whole process up:

Code:
java.security.AccessControlException: access denied (java.util.PropertyPermission user.dir read)
	at java.security.AccessControlContext.checkPermission(Unknown Source)
	at java.security.AccessController.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPermission(Unknown Source)
	at java.lang.SecurityManager.checkPropertyAccess(Unknown Source)
	at java.lang.System.getProperty(Unknown Source)
	at java.io.Win32FileSystem.getUserPath(Unknown Source)
	at java.io.Win32FileSystem.resolve(Unknown Source)
	at java.io.File.getAbsolutePath(Unknown Source)
	at sun.awt.shell.Win32ShellFolder2.<init>(Unknown Source)
	at sun.awt.shell.Win32ShellFolder2.listFiles(Unknown Source)
	at sun.awt.shell.ShellFolder.listFiles(Unknown Source)
	at sun.awt.shell.Win32ShellFolderManager2.get(Unknown Source)
	at sun.awt.shell.ShellFolder.get(Unknown Source)
	at javax.swing.plaf.metal.MetalFileChooserUI.updateUseShellFolder(Unknown Source)
	at javax.swing.plaf.metal.MetalFileChooserUI.installComponents(Unknown Source)
	at javax.swing.plaf.basic.BasicFileChooserUI.installUI(Unknown Source)
	at javax.swing.plaf.metal.MetalFileChooserUI.installUI(Unknown Source)
	at javax.swing.JComponent.setUI(Unknown Source)
	at javax.swing.JFileChooser.updateUI(Unknown Source)
	at javax.swing.JFileChooser.setup(Unknown Source)
	at javax.swing.JFileChooser.<init>(Unknown Source)
	at javax.swing.JFileChooser.<init>(Unknown Source)
	at ScreenCapturer.takeScreenShot(ScreenCapturer.java:46)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.plugin.javascript.invoke.JSInvoke.invoke(Unknown Source)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at sun.plugin.javascript.JSClassLoader.invoke(Unknown Source)
	at sun.plugin.com.MethodDispatcher.invoke(Unknown Source)
	at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)
	at sun.plugin.com.DispatchImpl$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.plugin.com.DispatchImpl.invoke(Unknown Source)
java.lang.Exception: java.security.AccessControlException: access denied (java.util.PropertyPermission user.dir read)
	at sun.plugin.com.DispatchImpl.invokeImpl(Unknown Source)
	at sun.plugin.com.DispatchImpl$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at sun.plugin.com.DispatchImpl.invoke(Unknown Source)

I am surprised that I don't have read permission although I did have write permission as implemented earlier. How do I give my applet read permission to the user's file system?

Your assistance is much appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top