?
Solved

Transferring Data between server and http client

Posted on 2005-05-03
17
Medium Priority
?
193 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:singhte
  • 9
  • 8
17 Comments
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13916711
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?)
0
 

Author Comment

by:singhte
ID: 13916914
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
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13917118
>>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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:singhte
ID: 13917610
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.
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13918940
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...
0
 

Author Comment

by:singhte
ID: 13924055
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.
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13924604
>>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.
0
 

Author Comment

by:singhte
ID: 13924915
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
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13924984
>>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...
0
 

Author Comment

by:singhte
ID: 13934342
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.
0
 
LVL 29

Accepted Solution

by:
bloodredsun earned 1000 total points
ID: 13934451
Save this as a jsp and try to run it:

---------
<%
response.setContentType ("text/plain");
response.setHeader ("Content-Disposition", "attachment;filename=\"resultfile.csv\"");
out.println( "hello,there,martin,how,was your,day"  ) ;
%>
--------------

all you need to do is to replace the String "hello,there,martin,how,was your,day" with the output from the session object.

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

This line tells the browser that it's an attachment to download and that the name of the file is "resultfile.csv". The reason it  is surrounded by \" is that you need to escape the qutoes around the filename in this case.
0
 

Author Comment

by:singhte
ID: 13935450
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

0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13936037
If you're using windows the line separator is "\r\n"

but you should use

System.getProperty("line.separator");

for portablilty across Os's
0
 

Author Comment

by:singhte
ID: 13944614
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?



0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13944717
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.
0
 

Author Comment

by:singhte
ID: 13944800
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>
0
 
LVL 29

Expert Comment

by:bloodredsun
ID: 13976479
>>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
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Integration Management Part 2
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question