Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Building a URL for a Servlet generated file

Posted on 2008-10-10
6
Medium Priority
?
407 Views
Last Modified: 2013-12-10
I have a Servlet that creates a CVS file based on some user specifications. The file is created but it is created on my weblogic domain root (user_projects/domain/mydomain)

The issue is that after I created the file I need to provide an URL for the user to download the file, since the deployment is on a WAR file it is not clear for me where should I store the CSV file for been able to provide a URL to the user.
0
Comment
Question by:ronenr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 19

Expert Comment

by:ramazanyich
ID: 22709143
why do you need to store CSV file locally on the server ?
you can directly stream generated CSV file to the client's browser.

Example code follows.



public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,  IOException{
   doPost(request,response);
}
 
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,  IOException{
 
     InputStream in =generateCSVFile(request);
     response.setContentType("application/csv");
     OutputStream out=response.getOutputStream();
     int k=-1;
     byte[] buf = new byte[10000];
     while((k=in.read(buf)) >0){
        out.write(buf,0,k);
     }
 
}

Open in new window

0
 
LVL 2

Author Comment

by:ronenr
ID: 22711695
More or less since I need to return a HTML page with information and that page should include the link to the file or the browser pop-up requesting to save or open the file. With your example  may  I also return the HTML page with the same stream?
0
 
LVL 19

Expert Comment

by:ramazanyich
ID: 22711860
you can return an HTML page with link again to the servlet URL with additional parameter for exmple.
and in the second invocation just do retrive csv and return it like in my example
0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 19

Accepted Solution

by:
ramazanyich earned 2000 total points
ID: 22711894
example:


public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,  IOException{
     if(request.getParameter("download")!=null){
     InputStream in =generateCSVFile(request);
     response.setContentType("application/csv");
     OutputStream out=response.getOutputStream();
     int k=-1;
     byte[] buf = new byte[10000];
     while((k=in.read(buf)) >0){
        out.write(buf,0,k);
     }
     }else{
         response.getOutputWriter().write("<html> .... <a href="/your/servleturl?download=true">Download csv </a>...</html>");
     }
 
}
0
 
LVL 2

Author Comment

by:ronenr
ID: 22712094
OK, what I am trying to understand is, Is there a way to store the file on the server and build an URL for it (so the user can click the link and get the file) or I must retrieve the file using a servlet as your example suggest. In the case that the only way is to use a servlet to read and send the file the question was answer but if there is a way I can store the file and provide a link to it more information is needed.
0
 
LVL 2

Author Closing Comment

by:ronenr
ID: 31505193
Although it do not directly answer the question about where to store the file on a Weblogic server I will use the solution suggested. Thanks for taking the time at looking at this issue.
0

Featured Post

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

-Xmx and -Xms are the two JVM options often used to tune JVM heap size.   Here are some common mistakes made when using them:   Assume BigApp is a java class file for the below examples. 1.         Missing m, M, g or G at the end …
Upgrading Tomcat – There are a couple of methods to upgrade Tomcat is to use The Apache Installer is to download and unzip and run the services.bat remove|install Tomcat6 Because of the App that we are working with, we can only use Tomcat 6.…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

670 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