Calling JSP from a batch file

If you wondering why in the world should I call a JSP from a batch file then read on. In fact, my problem was to schedule a task that runs every day on a specific time and must generate a report for my management and email the same. Searching the net I was able to accomplish this by combining VB and batch files. But I was not happy with the results. Or to be more specific, my management was not happy with the type of report I was generating in terms of formatting etc.

What the management wanted was a nicely formatted report just as our internal WEB based reports. Now I realised that I have already a JSP that generates the report, now I only have to do two things.

1. Modify the JSP to instead of displaying, just generate the report in HTML format and then email it
2. Secondly I must find a way to schedule this emailing to auto email every day at a specific time.

So I have answered the question as to the necessity of calling a JSP from a batch file. Now that I have decided on what to do, I set search on how to do this. Then I found that batch file by itself cannot accomplish this, therefore I would need a combination of a batch file and a vbscript file (VBS).

So assuming you have a JSP page that generates the report and emails it. Do the following to automate the report generation:

1. Create a text file and copy the following content into it:

Option Explicit
Dim objIEA
Set objIEA = CreateObject("InternetExplorer.Application")
objIEA.Navigate "http://192.17.12.10/emailreport.jsp" (This should be changed to your actual url)
objIEA.visible = true
While objIEA.Busy
Wend
objIEA.Quit
Set objIEA = Nothing

2. Rename the text file to report.vbs

3. Create a batch file and name it as report.bat and copy the following content in that batch file.

cscript.exe report.vbs

4. Create a Scheduled task from windows or cron job in your Unix servers to call this report.bat as per your requirement.

How to read and write in file through JSP

<%@ page import="java.io.*" %>


Read write file JSP



<%
String fileName=getServletContext().getRealPath("jsp.txt");

File f=new File(fileName);

InputStream in = new FileInputStream(f);

BufferedInputStream bin = new BufferedInputStream(in);

DataInputStream din = new DataInputStream(bin);
StringBuffer sb=new StringBuffer();
while(din.available()>0)
{
sb.append(din.readLine());
}

try {
PrintWriter pw = new PrintWriter(new FileOutputStream("c:/file.txt"));// save file
pw.println(sb.toString());
pw.close();
} catch(IOException e) {
e.getMessage();
}

in.close();
bin.close();
din.close();
%>
Successfully write file



Java Servlet to Write to a temporary file

/**
* Write to a temporary file
* @author Sanjeev Kulkarni
* @since 0.5 - 18-Nov-2009 3:23:35 PM
* @version 0.1
*/

package com.ack.web.servlet;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WriteToATemporaryFile extends HttpServlet {

public void doGet( HttpServletRequest req, HttpServletResponse res )
throws ServletException, IOException {
// get the web applications temporary directory
File tempDir = (File) getServletContext().
getAttribute( "javax.servlet.context.tempdir" );

// create a temporary file in that directory
File tempFile = File.createTempFile( getServletName(), ".tmp", tempDir );

// write to file
FileWriter fw = new FileWriter( tempFile );
try {
fw.write( "done and dusted" );
}
finally {
fw.close();
}

// tell servlet client where to look for file
res.getWriter().println( "check file: " + tempFile.getAbsolutePath() );
}

}

Happy Deepavali

Diwali or Deepaavali means an Array of Lamps i.e .Rows of diyas (Deep = Lamp, Vali =Array)

Of all the festivals celebrated in India, Diwali is by far the most glamorous and important. Enthusiastically enjoyed by people of every religion, its magical and radiant touch creates an atmosphere of joy and festivity.

As a family festival, it is celebrated 20 days after Dussehra, on the 13th day of the dark fortnight of the month of Ashwin (October / November).


It is a festival of lights symbolizing the victory of righteousness and the lifting of spiritual darkness. It celebrates the victory of good over evil - and the glory of light. This festival commemorates Lord Rama's return to his kingdom Ayodhya after completing his 14-year exile.

Homes are decorated, sweets are distributed by everyone and thousands of lamps lit to create a world of fantasy.

Deepaavali is a time for fun and revelry. Diwali is also a time for pooja and tradition. This Diwali festival, it is surmised dates back to that period when perhaps history was not written, and in its progress through centuries it lighted path of thousands to attain the ultimate good and complete ecstasy.

Deepavali is very enthusiastically celebrated for five continuous days and each day has its significance with a number of myths, legends and beliefs.

Jai Shri Laxshmidevi...

Happy Independence Day!!!

On Independence Day Here’s wising our dreams of a new tomorrow come true for us… NOW AND ALWAYS!!!

Take a stand against evil, corruption & terrorism 4 we belong to India, a nation of pride & we will thus say- "HINDU, MUSLIM, SIKH, ISAI, SAB HAI BHAI BHAI."
god bless all of us!!!

Happy Independence Day!!!

Orkut Scraps
Sanjeev Kulkarni