This week's book giveaway is in the Server-Side JavaScript and NodeJS forum.
We're giving away four copies of Modern JavaScript for the Impatient and have Cay Horstmann on-line!
See this thread for details.
Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!

Jack Tauson

Ranch Hand
+ Follow
since Mar 27, 2014
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Jack Tauson

Ron McLeod wrote:In addition to including Content-Type: application/octet-stream header, you also need to provide a header which provides the filename; something like:
    Content-Disposition: attachment; filename="fileName.zip"



Thanks. In my first chunk of code,I forgot to include the following ( I have edited my original post) for everyone's reference:



I wonder how do I include above stuff in my second chunk of code?

1 week ago

Paul Clapham wrote:Well, in the first chunk of code you use ".../custom_documents/" + user + "/" + filename and in the second chunk you use ".../custom_documents/" + filename + "/" + user. Those are both actual code accessing the file system. The first is correct and the second is wrong, according to both the error message and the description you gave before the first chunk of code. It is very simple to fix the second chunk of code to build the file path in the correct order.

It's true that your endpoint looks like ".../filename/user" but it's unfortunate, because your correct chunk of code extracts the filename and user parts and puts them back together in the correct order.



Thanks. That fixed the error after making the following changes. However, it's not downloading the actual zip file. It downloaded a file of type "File" with name "JACK" and when I opened it in notepad ++ it was full of garbage characters. What is going wrong here?

1 week ago
I have the following endpoint working fine if I run it locally like the following :



Here, I am running FileDownloadApp named application locally on eclipse which is deployed on tomcat.

-downloadFile is the name of the endpoint as shown below

-fileName.zip is the file residing inside /mnt/nfs/myfolder/prod/documents/custom_documents/JACK directory.

Basically, it's inside the JACK directory on Windows. The whole thing is meant for Unix environment but I am testing it on Windows and hence the pathname is like that.




However,when I converted it to the following - used Resource interface, I am getting the following error:




1 week ago

Dave Tolls wrote:Good job.

So does your endpoint return all the data in one go?



Thanks!
Yeah, it does. I guess as long as I am getting data back to the browser, I  will be able to download the data from the above logic.

Dave Tolls wrote:Yes, somewhere you need to remember what the query parameters were, either in the session or in the page.

Also, it doesn't have to be a flag....it can be another endpoint. It's up to you how to structure it.



I was able to achieve this using JavaScript only. Found this tutorial very useful:

https://www.youtube.com/watch?v=eicLNabvZN8&t=103s

Dave Tolls wrote:Well, it can be the same one with a flag or similar indicating what format to return the results in.



Hmm, so what I understood is that, for the table, I can leave it as JSON response.

For Export to Excel button, I could have a flag to call the same web service with a flag indicating that CSV data is needed. However, at the point, when button is about to click, in order for me to call the same webservice, I would have to remember somewhere in my UI application, maybe in the form of sessionStorage, the name of all the parameters selected/supplied by the user so that I can make a different call.

Are you also suggesting something similar to what I explained above?

Dave Tolls wrote:Can you show us a diagram of the architecture?
That is, what the different parts are and which bits you control?

I'm a bit confused as to which part is yours and which is some external service you call, and is not controlled by you.

Because, if I had control of this I would just have the download button request an excel copy off the server, and the server would redo the query, but instead of returning JSON it would create an XLSX file instead (or CSV if we're talking straight data, which would be dead easy).



Sure. Please find the UI diagram I just drew on a paper below:




So at first, user sees only the Advanced Search section until the Search and Clear buttons. After selecting things from the drop-down boxes, user clicks on the Search button. This calls a search web service in the backend. A new table shows up with Search results (81702) as shown in the diagram above.

Below the table, I have Export to Excel button. That's where I am having issues since the records are so big and jQWidget I am using is unable ot handle more than 600 records. So they are asking me to host their source code file on my server and so on and so forth.

When you say the following:

Because, if I had control of this I would just have the download button request an excel copy off the server, and the server would redo the query, but instead of returning JSON it would create an XLSX file instead (or CSV if we're talking straight data, which would be dead easy).



I would have to make another webservice call when user clicks on the Export to Excel button, right?
On my User Interface, there is an Advanced Search section where user selects 5-7 things from the 5-7 different dropdown menu and based on these parameters, a web service is called. The webservice returns the search results in a JSON format and those records are displayed in a tabular format in the User Interface.

Something like this : http://jsfiddle.net/jqwidgets/6HRU8)

So I have a Download button below the tabular section where all records are displayed, just like shown in the jsfiddle above.

If I have to download the records in excel format as soon as user clicks the download button, is there something better than using jQXwidgets that is used in the JSFiddle and in my code as well?

The reason I want to go away from jqxwidget is that they are asking to supply a URL hosted on my RHEL server and that’s causing issues to me in setting up virtualhost on Apache etc. The reason they are asking is that the records are around 2000 or more and to handle the load I need to have their solution on my server.

Tim Holloway wrote:

Dave Tolls wrote:Doesn't have to be multiple users, as in people, just multiple threads.

If more than one thread at a time accesses this code then something will eventually break.
All it would take is for one thread to close the connection before another thread tries to use either it or the Session value from lines 70-78.



Or for two threads to try to use the connection at once and thus corrupt the connection's internal state.



I see. How about using addressing multiple people accessing the application at the same time by adding synchronized block as shown below:

1 month ago

Dave Tolls wrote:I assume this isn't a threaded environment?



Nope. It's the thread that I have a discussion going on here.

https://coderanch.com/t/733391/frameworks/avoid-max-connection-warning
1 month ago

Tim Holloway wrote:As a general rule, a Factory is a service class that's designed to dispense resources efficiently. If the resources are poolable, then often the factory will maintain its own internal pool. For example the DataSource factory that's built into every J(2)EE-compliant web application server.



Thanks for the explanation. I will try to test the pooled connection factory approach that Stephan has mentioned in his code above.

And I won't even begin to consider what could happen if you have a single instance of something and multiple thread users without any synchronization.



Do you mean when multiple users are accessing the application at the same time?


1 month ago
I have a JMX connection defined in my code like this:

private static Connection connection;


I'm planning to check if the connection is open or close inside the finally block.

Is this the correct way to test?

1 month ago

Stephan van Hulst wrote:You should use a PooledConnectionFactory, and you should close your connections using try-with-resources, not with try-finally. You're also not using Spring's dependency injection mechanism properly.

Now the trick is to configure Spring that it creates a PooledConnectionFactory for your application once. You should be able to do it like this:



Thanks Stephan.

I tested a solution different than yours and didn't use PooledConnectionFactory as shown below and it's working fine:



So basically, the connection is cached  (e.g. in a static variable) and re-used as shown above. It didn't break through until now.

You'll note that in this code there is no finally block to close the connection. That's intentional because the whole point of this code is keep the connection open so that it's not opening & closing the connection to send a single message. The connection is being re-used between invocations. The only time the connection is closed is when a Throwable is caught.

Do you think I would still need to add a finally block, test for open connection and if it's open then close it? I wonder since finally block is always executed, it's going to close the connection after exiting the else block, right?

1 month ago

Stephan van Hulst wrote:Why would you want to store it in the database? Your client already has the data, as you are displaying it in your user interface.

I suggest that instead of your current approach, you offer the data directly to the client as a CSV file (instead of as JSON) and then you display the CSV in your user interface. Then when the user hits "Download" (or rather "Save", since you've already downloaded it), you can just save the CSV you're already displaying.



Hmm. I am using this (http://jsfiddle.net/jqwidgets/6HRU8) widget to display all data in tabular format and they are asking to supply a URL hosted on the server and that's causing issues to me in setting up virtualhost on Apache etc. The reason they are asking is because the records are around 2000 or more and to handle the load I need to have their solution on my server.

I will try to find some other  UI thing to display records and then look for download options
1 month ago
I have a spring boot app. So on the User Interface, there is an Advanced Search section where user selects 5-7 things from the text box and based on these parameters, a web service is called. The webservice returns the search results in a JSON format and those records are displayed in a tabular format in the User Interface. My goal is to download all the records into an excel file when a user hits a Download button.

So I have a Download button below the tabular section where all records are displayed.

If I have to download the records in excel format as soon as user clicks the download button, is there something like the following feasible or a good solution?

As soon as search results are sent back to the user interface in JSON format, I can have those records inserted into some temp table in the database. So when a user clicks on the Download button, using Java, I can grab all the records, convert it into CSV file and present it to the user for saving purpose. Please suggest if this is even possible or any other better approach .
1 month ago