Link to home
Start Free TrialLog in
Avatar of singhte
singhte

asked on

Transferring Data between server and http client

Hi
        I want to get the contents of  my server side query and store them as a file on the client side.

         What I am doing is as follows:

          I pass a set values from the form(jsp file) to the servlet, based on which a database query is done. Now I write the this queried data in the outputstream object of the HttpServletResponse response object. Now I redirect the page to another JSP file using RequestDispatcher forward function and try to access the outputStream of the servlet as input stream of the request object of the redirected JSP.

And  here is where the problem is occuring. It gives me an IllegalStateException. My research tells me that I cannot use RequestDispatcher because if the response object is not commited it gets cleared before redirection and if its is commited it gives an Illegal State Exception.

What I want is that data should be transferred back to browser window and I should see on  browser “Open with”, “save to disk” window.

Can anyone help me with this. He gets all the 250 points !!!!

If you need any clarification, mail me at tejbir_s@hotmail.com
Avatar of bloodredsun
bloodredsun
Flag of Australia image

In your servlet, you need to set the content-type correctly and say that it is a file e.g.

//the content type set as plain text or what ever you have
response.setContentType ("text/plain");
//the header and also the Name is set by which user will be prompted to save
response.setHeader ("Content-Disposition", "attachment;filename=\""+ yourFileName+"\"");

then do your output to the response, rather than forward to another JSP.

If you have already written to the response, you cannot forward to another JSP (and I don't understand why you would want to anyway?)
Avatar of singhte
singhte

ASKER

Basically mine is a GUI application where different jsp files send their requests to a controller servlet, which delegates the processing further to the respective java classes, after recieving data from the java classes, redirection happens to the jsp files where queired data is displayed on the browser window.

For one particular request I don't want to display data on browser window but store the data as a file on the client machine.
I am redirecting to jsp to display the index page of the GUI and in that jsp i am trying to get the response output stream as input stream of the request object.

Is this approach correct? Or is there some other way in which i can store data as a file on the client machine.

My content type in the servleet is set to "text/html; charset=BIG5" as i am also taking chinese characters in my applciation.

Any suggestion how it can be done.

Thanks
>>Basically mine is a GUI application where different jsp files send their requests to a controller servlet, which delegates the processing further to the respective java classes, after recieving data from the java classes, redirection happens to the jsp files where queired data is displayed on the browser window.

Sounds like you've written you're own version of Struts or Webwork.

>>Is this approach correct?

Not really, because as you mention, you will get an illegalStateException.

One possible way, assuming that the size of the file isn't too great, would be to put the file into the session object in the session, then forward to your presenting JSP (I assume that you use the request and session objects to pass data between your controller and your presentation layer JSPs amyway). This JSP then sets the correct headers such as content-type and charset and then retrives the file object from the session (making sure to remove it for good code hygiene and better performance) and then outputs it to the browser.
Avatar of singhte

ASKER

Thanks for your response.

Now what I am planning to do is that create a FileWriterObject, write data to it and set that in the session, but I am facing one problem. How will I read data from it to write to a file on the client side.

Can I use flush for that, if yes how can specify flush to write to my new file instead on the console.

Is there any other method for doing the same.
As you are just outputting a String, why not just save a String into the session rather then creating a File object?

in servlet
------------

HttpSession session = request.getSession() ;//get Session
session.setAttribute("resultfile" ,rs.getString(1) ) ; //put file from resultset into session
RequestDispatcher rd = request.getRequestDispatcher( "filedownload.jsp" ) ;
rd.forward( request, response );

Then in the jsp
--------------------
<%
response.setContentType ("text/plain");
response.setCharacterEncoding("UTF-8") ;
response.setHeader ("Content-Disposition", "attachment;filename=\"resultfile.csv\"");
String fileText = (String)session.getAttribute("resultfile") ;//get FileText
session.removeAttribute("resultfile")
out.println( fileText  ) ;
%>

And that's all...
Avatar of singhte

ASKER

OK , I got some idea about it

But How do make the window pop up says wether you want to save the file or open it with some software.

The one which pops up when u try to open any stored file in the yahoo briefcase.
>>But How do make the window pop up says wether you want to save the file or open it with some software.

I don't know about yahoo, but this feature normally depends on what mime-types the browser can handle. There's only 2 possible ways to do it. Both use setting the mime-type but one uses the Content-Disposition header, the other doesn't. IF you have a pop-up window saying this then you can offer the 2 ways to download.
Avatar of singhte

ASKER

Could not make much out of what u said in the last mail . I am using the latest version of IE.
Where should i set the mime types in the jsp or servlet
Can u give me some sample code
>>Where should i set the mime types in the jsp or servlet
>>Can u give me some sample code

Just try the code that I've already given you...
Avatar of singhte

ASKER

I am trying to create a file using the session approach but the file is geting created on the server side only.

What does this line mean

response.setHeader ("Content-Disposition", "attachment;filename=\"resultfile.csv\"");

what will the above line do.
ASKER CERTIFIED SOLUTION
Avatar of bloodredsun
bloodredsun
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of singhte

ASKER

Thanks Man !!!!!

It is workiing at last. But there is another issue now.

I am making a string from the data from a table. So if there 100 rows in the table, all of them are coming concated as a single string ( although i am putting '\n' character after constructing each line of data.)

Is there a way we can see the lines as '\n' terminated in the file.

RIght now (# is the field separator)

Hello#AMAN#AHello1#AMAN1#A1

Expected
Hello#AMAN#A
Hello1#AMAN1#A1

If you're using windows the line separator is "\r\n"

but you should use

System.getProperty("line.separator");

for portablilty across Os's
Avatar of singhte

ASKER

Hi
        I have created the my jsp as follows:

<%@ page import="com.hp.configurator.*,java.util.*,java.io.*" %>
<%
response.setContentType ("text/html; charset=Big5");

response.setHeader ("Content-Disposition", "attachment;filename=\"TableData.dump\"");

String dump = (String)session.getAttribute("DUMP");

session.removeAttribute("DUMP");

out.println( dump  ) ;

out.close();
%>
<form name="frm1" method="post" action="/Configurator/popUpMessage.jsp?Action=EXPORTDATA">
</form>
<script language="javascript">
      document.frm1.submit();
</script>


 The problem is as follows:

 When my server and client are on the same IP (Windows machine), it seems to working fine. The file is created properly with line separator.

But when I shift to the unix box, where my server is deployed on a different IP and i am accesing it through a browser from a different IP, the file is writing a single string instead of different lines.

Also the "form" part of my jsp is getting printed in the file. It happened on the windows also, but after i did "out.close()", form lines stopped coming in the file.

But on the Unix box it is still getting printed.

Any ideas how can go about it?



It's obviously just an OS specific thing if it's working on windows. What I think is happening is that t is being created on a Unix box with Unix separators but you are viewing it on a Windows box which does not recognise the line separators. What you will need to do is parse part of the request to get the users OS (such as the request header User-Agent) and create the file using the correct OS type separator.
Avatar of singhte

ASKER

Martin
            Thanks for your quick and good response, but thing is still there, how do i prevent the "form" data from printing in the file.

It is getting coming at the end of the file that is generated !!! The file looks like below.


TTASS#Tight Association.#01-01-2004#21-03-2005#
LASS#Loose Association#18-03-2005#18-03-2005#
PRQVS# PreRequisite VAS#01-01-2004#24-03-2005#
BPPE#BPPE#25-04-2005#25-04-2005#
AMAN#HELLO WORLD#05-05-2005#05-05-2005#
TEJBIR#HELLO TEJBIR#05-05-2005#05-05-2005#

<form name="frm1" method="post" action="/Configurator/popUpMessage.jsp?Action=EXPORTDATA">
</form>
<script language="javascript">
      document.frm1.submit();
</script>
>>how do i prevent the "form" data from printing in the file.

don't have it in the file!

You can't put char data in there if you've set the response type to non text

Bring it up in a pop-up window