I saw this link
How do I use getOutputStream() and getWriter() in the same servlet request?
and this link
best practice response.getOutputStream
I have tried something but can not succeed so far.
My doGet method
public void doGet(HttpServletRequest req, HttpServletResponse res){
this.doPost(req,res);
}
My doPost method:
public void doPost(HttpServletRequest req, HttpServletResponse res){
ServletOutputStream sos = response.getOutputStream();
PrintWriter out = new PrintWriter(new OutputStreamWriter(sos, "utf-8"));
if(something happens){
out.println(string_specific_to_situation);
return;
}
else if(some other thing happens){
foo(sos,other_string_specific_to_situation);
out.println(blabla);
return;
}
else if(some thing else happens){
foo(sos,else_string);
out.println(dotdot);
return;
}
}
A foo method
public void foo(ServletOutputStream sos,String str){
int length = 0;
InputStream is =null;
is= new ByteArrayInputStream(str.getBytes("UTF-8"));
//
// Set the response and go!
//
//
response.setContentType( "application/octet-stream" );
response.setContentLength( (int)str.length() );
response.setHeader( "Content-Disposition", "attachment; filename=\"" + name + "\"" );
//
// Stream to the requester.
//
byte[] bbuf = new byte[20];
DataInputStream in = new DataInputStream(is);
while ((in != null) && ((length = in.read(bbuf)) != -1))
{
sos.write(bbuf,0,length);
}
in.close();
sos.flush();
sos.close();
return;
}
As you can see, i want to make user side download a file, also i want to be able to show a message,
is not it possible?
Thanks