Downloading a file and forwarding to a page in Struts - java

I have a problem about using Struts to download a file. I know Struts is an old technology, but my company is maintaining an old application for a customer.
So, problem is that we want a webpage to download a file. Scheme is the following : Reach for webpage > click a button > execute treatment (DB query and data selection) > produce excel file > download the file > back to initial page.
My code is working until the file download, i.e. I can download a correct excel file, but I can't go back to the original webpage.
I read this answer and others in this forum, and it seems that downloading a file and going back to original page are 2 different treatments, and that Struts can only process one...
I absolutely need those 2 treatments, so my idea is the following : When the user clicks the button on the original page, I should open another window (or even a pop-up) which will take care of the excel treatment and download, and my original page won't move, therefore I don't need to forward at the end of the treatment.
Question is, do you think this is viable ? I couldn't find a similar code here so I guess it isn't a good idea, so if you have a good practice about my need, please share it :)

Yes sure your idea will work. But the user has to close the pop-up after the download starts.
Just to give some other ideas:
I had a similar problem once and solved it a little bit different. Javascript does allow execution of multiple statements in one row with one button (what you would do anyway with the popup and redirect solution).
So you could listen on the click event on the button and download the file and do something different after that.
Here are some topics about downloading files with javascript Link without using a popup. And a simple javascript redirect is also very easy Link #2.
The glue code to struts are two links you have the generate while the user visits your page.
More or less pseudo-code in html/jquery:
<button id="download" data-downloadlink="<s:property value='downloadlink'/>" data-redirectpage="<s:property value='redirectpage'/>">Download</button>
$('#downloadbutton').on('click', function() {
download($('#downloadbutton').data('downloadlink'));
redirect($('#downloadbutton').data('redirectpage'));
});

Related

Saving a information displayed as different tab in browser to .html file

I am working on a project which is developed with Java Struts 2 framework. I am completely new to this. In project, when user clicks on a report button, a report is shown in different tab (the url of it looks like https://localhost:8181/myApplication/businessReport.do).
Goal: What I want is saving this report as html file in backend somehow. It is perfectly OK when user sees the report as he is seeing currently. It just needs to be saved in backend somewhere. The reason to do this is, te application is going to shutdown soon and our client wants to save those reports somewhere for future reference.
I could find businessReportForm.java, businessReportAction.java and also businessReport.jsp files in my project. I assume they should be of my interest.
Note: It uses mapping.findForward from Action mapping
I do not understand following and it would be nice if someone can help with that.
What could be the last point in (with respect to strut framework) where all the information for report would be ready ? (I would lke to use it to create .html file out of it)
What I need to change here in order to achieve the goal ? changing .jsp , changing Action or should change be done in corresponding Action class ?
in the url what does businessProcess.do actually mean with respect to jsp and servlet ?
any other idea what should be the approach to do what I want to do ? (saving report as html in background)

Dialog box in Spring MVC controller

Is there a way to display a (preferably modal) dialog box in an servlet controller ? If it isn't modal that is something I can deal with (!?) as long as it initially appears above the browser.
Essentially I have a form with a table and 2 buttons on it. One button takes the user to a different place in the workflow and is irrelevant to this question (just to explain why the other one doesn't 'go anywhere').
The other button currently goes back to an MVC controller, calls some code to export the table to excel and then reloads the web page. This is all working okay except the way I am calling the dialog box is calling it underneath the browser. I suspect this is because I am sending null as the frame but I'm not sure what to put in its place ?
JOptionPane.showMessageDialog(null, "Export Completed.", "Excel Export", JOptionPane.INFORMATION_MESSAGE);
Many thanks.
I am not too sure what you are trying to achieve from your question and without any code examples. However, presumably what you are doing (guessing here), is that you are trying to export some data from some data source and convert it to an Excel file. You have to keep 3 things in mind.
Web applications work via HTTP requests and responses. So the only thing a Servlet can do is send back an HTTP response that indicates that the export was successful. Whether you use a traditional page, or maybe use AJAX to avoid refreshing the page is purely your design choice. You could also start with a simple page and then change to AJAX combined with JQuery later once you get used to what is happening.
Exporting the excel sheet to the server does not mean that your client has access to it. Speculating here, but you will probably need a mechanism for your user to get the file. One simple approach used by many webapps is to actually send the Excel file as the Servlet response itself. So what would happen is that when the export is completed the browser starts receiving the file and the user sees it downloading. From your servlet you will just need to set the right mime-type and set the content-disposition header to state that the file is an attachment (so that the browser downloads it as a file).
httpresp.setContentType("text/csv");
httpresp.setHeader("Content-disposition", "attachment; filename=\"export.csv\"");
You will probably also need to set the file size. There are various full examples on SO if you look for further details.
When performing these operations remember that users can interrupt the browser or refresh. If the user presses F5 he might cause your application to do the export again. One common approach to this is called 'redirect-after-post'. Basically you redirect the user to a page which just displays the outcome, without performing the operation again. This way if he presses refresh, he is just refreshing the page with the message.

Navigating a website and uploading a PDF using a submit button using JAVA

To explain my situation, I currently have a large amount of PDFs (In the hundreds). Each PDF has a name associated with it and I need browse an external website my job uses to manage their files manually, clicking a button to "attach file" every time and manually selecting the file to upload to the site. If this sounds incredibly inefficient that's because it is, and doing this takes hours to finish while more of them pile up.
I already know how to deal with files, moving them around on a computer, and modifying them using JAVA, but I haven't done anything related to interacting with websites so I wouldn't know where to start.
I'll need to be able to perform the following actions or actions similar in order to complete what I have in mind.
General browsing of a website
Navigating through the website using
links provided by buttons on the page
Being able to click buttons.
Reading Strings present on the screen in order to compare names (Under a certain section there is a list of peoples names. If I detect that there are multiple people with the same name I want to skip the file and deal with it manually)
Clicking a button which brings up an upload menu and selecting a file to upload to it, or any other way of interacting with a feature such as this. (Like when you click Computer on IMGUR and it asks you to choose an image you would like to upload)
I'm not asking for anyone to straight up give me the answer I'm looking for (Though I'm not opposed if you are aware of how to do things such as these and would like to share.) but any guidance on where to find information on performing such actions would be helpful. I've already been searching and will be continuing to search for relevant information.
Thank you for any help you may be able to give.
It appears you want to automate some manual steps on a browser. You can take a look at Selenium WebDriver.

Chrome/Firefox web browser automation for collect data

I would like to browse automatically in a website to collect some data.
There's a page with a form. The form consists of a select and a submit button. Selecting an option of the select and clicking on the submit button leads to another page where there's some tables with related data.
I need to collect and save in file this data for each option. Probably I will need to go back to the first page to repeat the task for each option. The detail is that I don't know the exactly number of options previously.
My idea is to do that task, preferably, with Firefox or Chrome. I think that the only way to do that is via programming.
Someone could indicate me a way to do that task in a easy and fast way. I know a little bit about Java, Javascript and Python.
You might want to google "web browser automation" tool like Selenium. Although not entirely fit for the purpose I think it can be used to implement your requirement.
Since the task is relatively well constrained, I would avoid Selenium (it's a little brittle), and instead try this approach:
Get a comprehensive list of options from the first page, record that in a text file
Capture, using a network monitoring tool like Fiddler, the traffic that is sent when you submit the first page. See what exactly is submitted to the server - and how (POST vs GET, parameter encoding, etc).
Use a tool like curl to replay the request steps in the exact format that you captured in step 2. Then write a batch script (using bash or python) to run through all the values in the text file from step 1 to do curl for all the values in the dropdown. Save curl output to files.
I found a solution to my problem. It's called HtmlUnit:
http://htmlunit.sourceforge.net/gettingStarted.html
HtmlUnit is a "GUI-Less browser for Java programs".
It allows to web browsing and data collecting using Java and it's very simple and easy to use.
Not exactly what I asked, but it's better. At least to me.

Java Applet: Edit and upload changed documents

In a work-related project, we want users to be able to write a document (in say Word) then upload this to our central web server. When they want to make changes, they visit the website and downloads the document. When changes are registered, the document is uploaded automatically.
I have written a few Java classes that does the work for me (FTP upload/download, File monitoring etc), but I'd like some input on how to solve the practical part of it.
Should the web page include a Java Applet that contains a Download button? When clicked, the document is downloaded and the associated editor is opened.
If so, how should I proceed? If the user closes his browser, wont be applet be terminated as well?
I have never worked on a such project before, and since Stackoverflow is filled with incredible smart people, maybe some of you could come up with some ideas?
PS: I'm new to posting on Stack, so please let me know if I should be more specific / add more information etc. :)
Best regards!
EDIT:
The application is only going to be used internally in our offices. Sorry if I caused confusion. :)

Categories

Resources