How to make the Open/Save dialogue download the text file instead of JSP
I am currently coding on a JSP program, which searches the database and writes the resultset into a file on the server, and then allows the client to download this tab delimited text file onto their local machine. But I met a problem, when the default Open or Save dialogue appears, it shows that it's trying to download the running JSP file from my local host instead of the newly-created text file. Despite this, when I click OK to either Open or Save the file, a warning dialogue will appear saying: The explorer cann't download this file, it's unable to find this internet site or something like that. I get no error message from the server but I was always told that Javax.servlet.ServletException: getWriter() was already called. What does this mean?
I guess maybe this is caused by the mix use of outputStreams in my program. I don't know if there is a way to directly read the resultset from the database and then send it through outputStream to the client. My solution is: first create a file on the server to hold the resultset, and then output this file to the client. I did all these in one JSP program: Create file on the server, search database, and then read file and output the contents to client. Is this correct? I attached my code, please feel free to correct any of my mistake? Thanks!
//global.class is a class dealing with database connection
<%@ page language="java" import="java.sql.*,java.util.*,java.math.*,java.io.*,ises.*,frmselection.*" %>
<jsp:useBean id="global" scope="session" class="ises.Global" />
/>
<!--start to process data-->
<%
//get query statement from the session
String sQuery = "";
if (session.getAttribute("sQuery")!=null && !(session.getAttribute("sQuery").toString()).equals(""))
{
sQuery = session.getAttribute("sQuery").toString();
}
String path = "c:/temp";
String fileName = "temp.TXT";
File file= null;
FileOutputStream fo = null;
PrintStream ps = null;
try {
file = new File(path,fileName);
if(file.exists()) {
file.delete();
file.createNewFile();
}
fo = new FileOutputStream(file);
ps = new PrintStream(fo);
}catch(IOException exp){
System.out.println("IO Exception: " +exp.toString() );
}
java.sql.ResultSet recResults= null;
java.sql.Statement STrecResults = null;
STrecResults = global.getConnection().createStatement();
recResults= STrecResults.executeQuery(sQuery);
ResultSetMetaData meta = recResults.getMetaData();
int columns = meta.getColumnCount();
String [] tempColumnName = new String[columns];
String [] ColumnName =null;
int DisColumns = 0;
int unDisCol = 0;
String sLine = "";
if(recResults.next()) {//if_1
for(int n=0;n<columns;n++) {
String temp = meta.getColumnName(n+1);
if(!temp.equals("PROJECTID")&&!temp.equals("BUILDINGID")&&!temp.equals("HAZMATPROFILEID")) {
sLine = sLine + "'" + temp + "'" + " ";
tempColumnName[DisColumns] = temp;
DisColumns ++;
ColumnName = new String[DisColumns];
}else {
unDisCol ++;
}
}//end for
for(int i=0;i<(columns-unDisCol);i++) {
ColumnName = tempColumnName;
}
ps.println(sLine);
do{
sLine = "";
for(int n=0;n<(columns-unDisCol);n++) {
String tempColName = recResults.getString(ColumnName[n]);
if(tempColName==null) {
sLine = sLine + "" + " ";
} else {
sLine = sLine + "'"+tempColName+"'" + " ";
}
}
ps.println(sLine);
}while(recResults.next());
}//end if_1
recResults.close();
recResults = null;
STrecResults.close();
STrecResults = null;
%>
<!--end of processing data-->
<!--start of download.jsp-->
<%
//set the content type to text
response.setContentType ("plain/text");
//set the header and also the Name by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment;filename=temp.TXT");
//Open an input stream to the file and post the file contents thru the servlet output stream to the client
InputStream in = new FileInputStream(file);
ServletOutputStream outs = response.getOutputStream();
int bit = 256;
try {
while ((bit) >= 0) {
bit = in.read();
outs.write(bit);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();
%>
<!--end of download -->

