Java, Struts and Outputing an Excel File
In a project I was working on last night I needed to output sets of data on demand in excel. I found various ways to do this however this was the most simplistic and worked every time on all my computers. (Mac, Linux, XP).
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","inline" );
Those 2 lines make the instant translation of the HTML I put in the page go to excel. Now something massive to remember here: The formatting is pretty unpredictable if you use css or any fruity styles. Luckally in my case I didn't need to use either; just output the data.
In either case you simply draw the HTML as you would to output a basic page with the response lines telling your browser that excel owns the page.
Absolute Link
This is a fantastic link for finding information about EJB and Hibernate Annotations. So thanks to the guys/gals that produced it:
Applet File Drop
I needed to make a way for a person to drag a file off of the desktop and onto the browser using a java Applet. And this is the code to make that happen:
public class SomeApplet extends JApplet implements DropTargetListener {
The first step is to implement the DropTargetListener as you extend JApplet (or Applet). This is going to provide the applet the ability to contain a Drop Target. That is right, I said "contain". I have tried this attaching the Drop Target Listener to the Applet itself unsuccessfully however; it attaches fine to a JPanel or even a Label.
JPanel jp1 = new JPanel();
DropTarget dt2 = new DropTarget(jp1, this);
jp1.setDropTarget(dt2);
Next declare the panel and a Drop Target then attach the drop target to the panel (once again this could be done with a label or whatever).
public void drop(DropTargetDropEvent dtde) {
int action = dtde.getDropAction();dtde.acceptDrop(action);
fromTransferable(dtde.getTransferable());
dtde.dropComplete(true);
}
First you get the Drop Action from the dtde object which represents the Drop Target Event then accept the Drop using the acceptDrop function. This registers the accepted drop with the applet. Once you accept then you dispatch the function which handles the drop (uploading the file in our case) then tell the applet the drop is complete using the dropComplete function. This is a great place for error handling to return false if there is an error or true if the drop function was successful. Both will have different display consequences which can be seen through testing
Object data = t.getTransferData(DataFlavor.javaFileListFlavor);
java.util.List<File> fileList = (java.util.List<File>) data;for (File file : fileList) {
if (!is_in_list(file_names, file.getAbsolutePath()) && !file.isDirectory()) {
Lastly we cycle through the list of files we got and decide whether to save them or what to do.
And there you have it: a medium level overview on how to drop a file into the browser on an applet and have it upload.
Applet and Javascript
So you have an applet and you want to do one or both of the following things:
- Run javascript commands from within your applet
- Control your applet through javascript
Step 1 Setup the Object Tag:
<object type="...">
<param name="mayscript" value="true">
<param name="scriptable" value="true">
</object>
*Note - I did not show the archive, code or other params needed to setup an object as they are dependent on your applet.
*Note - This setup allows for javascript communication in and out of your applet
Step 2 From within the java call some javascript
import javascript.*;
....
JSObject win = JSObject.getWindow(this);
win.eval("alert('test');");
*Note - To make this work you need to add plugin.jar from within your jdk/lib folder in the project classpath and include it in your deployment (archive = "..., plugin.jar").
And tada you can call java functions (yes the public ones) from javascript just by referencing the id of the object (dot) the function name. The translation between object types can get a bit tricky if you try to do a map or hash but with strings, ints and the like its pretty straight forward. The primary use for this was in setting up an event and using javascript to trigger it on submission of a form however the possibilites are endless
Oracle Blob (JDBC) – How to
This morning I needed to insert a file's contents into a blob on an oracle database so here is the method used to do that (look for a future post on dropping a file into an applet).
/* File Loaded here into a File Object as f */
PreparedStatement ps = conn.prepareStatement("INSERT INTO TBL VALUES(?, ?)");
ps.setString(1, f.getName());
FileInputStream fis = new FileInputStream(f);
ps.setBinaryStream(2, fis, fis.available());
ps.execute();
ps.close();
And tada your record will be loaded into the blob.
*** Not Shown ***
Oracle JDBC Connection - This example does not use any framework/connection helper (i.e. Hibernate/EJB)