File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes print Screen from a jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "print Screen from a jsp" Watch "print Screen from a jsp" New topic
Author

print Screen from a jsp

Amit Hetawal
Ranch Hand

Joined: Apr 12, 2008
Posts: 48
Hello All,
I have an email application, which is hosted inside a jsp page, which is displayed to the users to send any error report to the developer from the application itself.
Everything works, since i have a textarea inside the jsp where the users type, the text message. I wanted to know is there a way if i can get the screen shot of the user desktop windows in that email message which is being prepared. So basically if the user presses printscreen, is there a way i can get that screencapture in the text area or any other HTML object so that i can send that message out from my jsp code.

So i was thinking in lines of any javascript if i can form my message in such which can incoporate this pritn screen in any kind of DOM object to be passed to the jsp.

Please advice,

Thanks,
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61770
    
  67

Sorry, no.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
Wow...I have exactly the same requirement.

I need an e-mail shot off with a screen cap in a JSP application. So is there absolutely no way - JSP or otherwise - to do this?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The main issues are

(1) Platform dependence. There's simply no standard, cross-platform screenshot API you can invoke. Someone may have written a portable library of some kind, but it's unlikely to be installed on everyone's machine. Which brings us to...

(2) Security. I hope that a moment's reflection tells you why it would be a bad idea to empower arbitrary Javascript apps to make screenshots of your desktop. There's no way to talk to that cross-platform library, if it even exists, without somehow getting around the security machinery. A signed Java applet can do it, with considerable pain again for the cross-browser issues.

Now, if this is an Intranet application where all the desktops are Windows, and you can install a java.policy file on each machine, then the path is clear -- it's just a Small Matter of Program. But for the whole problem in its terrible generality, the problems are likely to be insurrmountable.


[Jess in Action][AskingGoodQuestions]
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
Yeah, I appreciate the problems when the platform is not standardized.

I can reasonably generalize for the purposes of my application that it will be Windows 2000/Windows XP (more so the latter) and the browser will be Internet Explorer 6.0.

I gather from a preliminary look on the internet that VBScript accomplishes the opening of the default mail client best (It will be Outlook 2003 for my purposes). I don't need to configure server-related credentials. All I want to do is open Outlook with a pre-meditated addressee, subject and the attachment (the same way a mailto: link functions, but it doesn't support attachments unfortunately)

I have successfully implemented screen capturing in Java/JSP. It is the sending of the mail with the screen caps as attachments that is the stumbling block now. Appreciate any pointers on this one.
[ June 17, 2008: Message edited by: Sridhar Venkataraman ]
Amit Hetawal
Ranch Hand

Joined: Apr 12, 2008
Posts: 48
Hi Sridhar,
Since I started this thread, let me just add what i come up with. I have implemented, the screen capturing using the standard java api, which created a screenshot.jpg file. Now for me since this location will be constant every time, so when the user click the send email button, this triggers the fileupload action in the background of this screenshot.jpg file on the server, as the mail code is present in jsp,with the help of some flags i detect if the file is uploaded if yes, i just send it from the java mail as attachment. basically you need to get some user action to trigger this email send.

Hope this makes sense.


Thanks,
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
Are you storing the screenshot on the server or on a location on the user's workstation?

For me, I am throwing up a Save dialog asking the user to save it. As soon as they do that, I need to trigger off an e-mail with that file as an attachment.

The problem is that the Java Mail API is cumbersome to use with Outlook. VBScript works superbly, but I can't make a link between the capturing of the screenshot (which is a JAR file implementation...it triggers when the "send e-mail" action is performed by the user) and the running of the VBScript to attach this to a mail message in the JSP application.

Thanks for your inputs anyway.
Amit Hetawal
Ranch Hand

Joined: Apr 12, 2008
Posts: 48
Hi,
So originally the file is stored on the user workstation, but once the user click send email button, i post that file to the servlet, where its written in a temp file and sent out as an email attachment, and i delete once email is sent in the same code.
And for the screen capture i just use on java class to achieve it, wonder what your jar implementation of screen capture provides ??

I really wanna hear about your approach, just to know the alternatives,

Thanks,
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
I am still stuck on the implementation.

The JAR provides a screen capture, but I am unable to fire up my default mail client (which is Outlook) using the Java Mail API (it is quite cumbersome to define SMTP settings and the like..).

I have VBScript which can open up a blank message with initial parameters (To, CC, BCC, Message Body) etc. and an attachment.

The problem is that I need to execute the two together when the user clicks a link on my JSP page.

As far as I know, Java cannot interact with VBScript. I tried registering the Java class as a COM object and then using the object inside VBScript code, but I learnt that Microsoft support for the MSJVM has been ended as of 31 December 2007.

Another way I can do this is to create a batch file on the server. The batch file will first execute the java program that captures a screenshot and places it in a predetermined location on the user's computer. It will then invoke the VBScript to attach this screenshot and open up a mail message like the one talked about above.

Now the problem is figuring out how to run a batch file when a user clicks on a link. Any pointers on that?
[ June 18, 2008: Message edited by: Sridhar Venkataraman ]
Amit Hetawal
Ranch Hand

Joined: Apr 12, 2008
Posts: 48
Hmm,
So, i dont know how UI-tically this will be feasible for you. But to trigger your two actions you may need two user intervention. So in my jsp, i have this button which says report problem, when the user clicks it, i trigger the screen shot capturing, and save the file in the desired location. Once, the user fills in the text area for the email, and clicks the send email button on this new jsp, i upload that screenshot(if it exists) and the text area data to the server, for sending out the mail.

You can do something like a onload of jsp, for triggering of the screen capture,


P.S. I use an applet to get the control of user system or the screen capturing.
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
Hi Amit,

Your solution is good, albeit fairly complex. My requirement is for an easily maintanable solution, though it may be scrappy.

I have made a few modifications to the architecture such that I don't need to run a batch file directly when the user clicks a link, but rather invoke a Java class which can run a batch file. But this too is posing some problems for me.

I have posted a thread in this regard here: http://www.coderanch.com/t/385655/java/java/Error-running-batch-file-Java

Thanks for looking into this.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi Amit and Sridhar,

Can you guys send me the code of screen capture? Will it be able to make image of a webpage?

I will be very grateful


For batch file execution on windows use my class here
Thanks in advance,

Maki Jav
[ June 24, 2008: Message edited by: Maki Jav ]

Help gets you when you need it!
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
I am attempting a roundabout implementation of the screen capture. Will be glad to share my code with you if I succeed.
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
Following on from the last part of the discussion HERE, this is a stacktrace retrieved from the Java Console:



This shows that the method takeScreenShot() is being invoked (this contains the screenshot taking code and is using the java.awt.Robot class), but access is being denied.

How - if at all it is possible - do I sign my applet or modify the code in my applet in such a way that the javascript can invoke this method successfully?

NOTE: I want to save the screenshot on the client's workstation and attach it to a mail message.
[ June 26, 2008: Message edited by: Sridhar Venkataraman ]
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
I managed to make this baby work!

I think what clinched it was the privileged code clause. Thanks everyone fo the help extended. I greatly appreciate it.
Sridhar Venkataraman
Ranch Hand

Joined: May 22, 2006
Posts: 76
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:



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? It is self-signed and all the code which accesses the user's file system is implemented as privileged code.

Your assistance is much appreciated.

UPDATE:

I managed to bypass the security restriction (It surprised me because I did not explicity do anything in the code which dealt with security).

I now have a new problem. When I fire up the java applet using the javascript, it throws the file save dialog and then appears to freeze (the dialog is not immediately in focus and I have to alt-tab around to bring it into focus).

Upon typing a file name, the javascript throws up an "object expected" error.
[ July 02, 2008: Message edited by: Sridhar Venkataraman ]
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
It seems that what you have declared in your html eg some method, it is not visible to javascript. Applet should be declared first ie <Applet> tag.

Maki Jav
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: print Screen from a jsp