2

I'm learning the D3.js library. How I can send a file with an httpRequest as described in this sample: chart?

I have a local server tomcat within eclipse. Is it possible to use this?

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
     //send file from here
}

And then catch it from:

d3.tsv("data.tsv", function(error, data) {
    if (error) throw error;
Anders
  • 8,307
  • 9
  • 56
  • 88
OiRc
  • 1,602
  • 4
  • 21
  • 60
  • Its not a post it will be a get. – Cyril Cherian Sep 13 '15 at 08:24
  • @Cyril thanks for the comment, can you provide me a little snippet? – OiRc Sep 13 '15 at 08:27
  • @Oirc: Do you want to upload the file from Javascript to Servlet Or Do you want to send file from Servlet to Javascript? – Ravindra babu Sep 13 '15 at 09:12
  • @Cyril I want to send file from Servlet to Javascript – OiRc Sep 13 '15 at 09:16
  • Have a look at: http://stackoverflow.com/questions/408735/javascript-file-uploads and http://www.ajaxf1.com/tutorial/ajax-file-upload-tutorial.html# – Ravindra babu Sep 13 '15 at 09:27
  • You can get file directly through javasacript. What is your exact need? Why servlet should send file to javascript? – Ravindra babu Sep 13 '15 at 09:30
  • @sunrise76 because files are stored in my local machine – OiRc Sep 13 '15 at 09:33
  • @sunrise76 i need an example, for doing that-> given this [this](http://bl.ocks.org/mbostock/3883245) – OiRc Sep 13 '15 at 09:35
  • If files are stored in your local machines, you can directly send file to javascript with out using servlet : http://blog.teamtreehouse.com/uploading-files-ajax. I doubt that we can call javascript from servlet. http://stackoverflow.com/questions/10258425/how-to-call-function-of-javascript-from-servlet – Ravindra babu Sep 13 '15 at 09:37
  • @sunrise76 i need tomcat to run below this? and can you show me an example on your answer? – OiRc Sep 13 '15 at 09:40
  • Updated answers. You need tomcat to run below example. But change your design for file upload since you are copying local file. Local file can be uploaded via Servlet or AJAX – Ravindra babu Sep 13 '15 at 09:58

1 Answers1

1

By using Apache common jar, the sample code will be like this

if(ServletFileUpload.isMultipartContent(request)){
   try {
            List<FileItem> multiparts = new ServletFileUpload(
                                     new DiskFileItemFactory()).parseRequest(request);

            for(FileItem item : multiparts){
                if(!item.isFormField()){
                    String name = new File(item.getName()).getName();
                    item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
                }
            }
           //File uploaded successfully
           request.setAttribute("message", "File Uploaded Successfully");
        } catch (Exception ex) {
           request.setAttribute("message", "File Upload Failed due to " + ex);
        }        
     }

Have a look at File Upload Servlet for complete code snippet

File upload with AJAX :AJAX file upload

EDIT

Calling javascript from servlet:

   request.getRequestDispatcher("/some.jsp").forward(request,response)

In this jsp, just call Javascript.

But this is not a good design. Servlet is executing at server and Javascript is executing at client end. If you want to upload file from your local machine, you can use File Upload Utility of Java Or Javascript. Calling Javascript from Servlet is not right thing.

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211