We have a form which once submitted a file is created and returned. I created a java method which does the post and a ok status is returned. However how am i able to download the file after the post?
Sorry for not being clear its driving me crazy. We have a business object which generates reports based on parameters sent to it. Once the form is filled in the browser a pop up comes up (save/open) file. What i want to do is create a java standalone program that will sit on my desktop so that when I run this programing (passing it my name and password and URL to post to, this is done already) it will download the file that is created on the server side. The problem is that I don't know where the file is stored (if it is stored) on the server or the name of the file. All i know is that on the browser we go to the form fill it in and the file is returned to the browser. So far the post is working.
When you are on the form in the browser (e.g. http://localhost/my/form) you should inspect the source of the page (IE is Menu View > Source ). In the source you should search for a form tag. This tag contains an action value like:
<form action="myaction.dhtml" method="...>
</form>
So the URL to request is http://localhost/myaction.dhtml and the servers response will be a "file". Good.
You may send the same request that does the browser from Java. To not code all that stuff again you may use a library like HttpComponents.
Probably your form is sending parameters too to the server (user name, password, etc). Look at the form components what parameters the server expect. Your URL may looks like this:
http://localhost/myaction.dhtml?name=Joe;pass=myPassWRD
You don't have to know where the file is stored, but you will need the correct URL that the server will use to take or generate the correct data and send to the client.
Related
We have a GWT app that accesses REST API. REST API is meant for other uses too, and is secured using JAAS basic authorization. GWT app uses RestyGWT dispatcher and filter, as shown here, to add Authorization: Basic to header, and, so far, this works fine.
However, our app also allows users to work with files, either download generated (such as pdf reports) or upload/download any kind files. We used servlets on server side for upload and download of these files, and Window.open() call to receive them in GWT. Without JAAS this worked fine.
Now I'm trying to secure that part of the API, too. Window.open(...) won't work because it doesn't allow for adding headers.
Is there some kind of workaround for this?
I've tried RequestBuilder, and I receive correct response, that contains the requested file. However, I'm not able to initiate the download of the file. Is it maybe possible to encode this (AJAX) response as data:... URL and display it using, for example, iframe, which will, in turn, initiate file download?
I always handled downloads by producing regular links. Wouldn't it be ok to check if a user session is valid and only then deliver the file?
By the way you could obtain an opened window's document and populate it:
public static native Document open(String url, String name, String features)/*-{
return $wnd.open(url, name, features).document;
}-*/;
I am new to web programming. My web application can upload files (uploaded by drag and drop method in javascripts ) and i want to retrieve them in servlet using Json . servlet file only needs the contents of the text files to do the calculation.
Can any one suggest how to do this ?
softwares used - netbeans ,tomcat
Thank you.
I'm not quite sure if you mean rendering your files in servlets or actually downloading them from your browser. I'm going to assume you mean rendering them. If so, then what you have to do is set up a URI which is associated with the content you want to render. Let's say this is a simple "hello world" rendering in the browser. what you would do is set up a URI as such:
localhost:3000/helloWorld.html.jsp
What you do on your back end is then wait to receive a http GET request to "/helloWorld.html.jsp" page, and then have your router send over the correct page. Luckily, with tools such as tomcat, the routing from local host is straight forward enough, and as your comments mentioned this is simple enough to do with the ample resources on the web.
Also an important point, you don't need JSON when using servlets (if i understood your problem correctly). If you want to send over data in a JSON format, then all you would do is modify the above steps to listen for "/sendJSON.html.jsp" (this could be an empty jsp), and you send over JSON from your back end in your response.
The idea is that you can add something to a database, which goes from browser -> java code -> JSP -> java code -> database, and you are then redirected to a page containing the information you sent. The servlets are in place but I cannot redirect to the HTML page from a get request.
I have a servlet to PrintWriter().print() the data in a Json object, but that servlet is called from the javascrit within the HTML page. How can I send the HTML page? Should I parse the HTML page and PrintWriter().print() each line? Is there a more proper way of doing this?
Keep in mind that sending HTML straight from JSP is not an option, and I can't change the structure of the system.
edit: Sorry, I typed that in a rush.
As a preface, the system is similar to StackOverflow, whereby you can submit a 'request' which prompts the community to crowd-source learning material.
Right now, the structure of the system is JS/HTML on the browser side, which communicates with a mySQL DB through an API written in Java. The API goes through JSP which communicates with an inner Java API for accessing the DB. The catch is that I must return Json objects from the API. I know that JSP is essentially useless and I could interface the two APIs without JSP, but this is a first year college project so I don't have the choice.
When you submit something to the database using the url /addrequest (or similar), the system puts the text into the database and then redirects you to /request/idnumber. When you access the /request/* URL, another servlet runs. I want this servlet to tell the browser to open my "request_display.html" page. Then the javascript on that page will call another url to get the Json object through the API, and then it will build the page.
I don't know how to tell the browser to open a html page. Should I just parse the html file and then use response.GetWriter().print() to do send the HTML?
If you are in a Servlet:
response.sendRedirect("pathOf YourHTMLPage");
If you are in a JSP page, try using a form or a "a" element. Like this:
<form action="nameOfYourServlet"></form>
or
Can't really understand what you are looking for but if you want to redirect user to an html page using servlet this can be done using response.sendRedirect("path to html");
It would be nice if you could explain via some code as your English is hard to understand.
response.sendRedirect("redirect.html");
Alternative way
ServletContext sc = getServletContext();
sc.getRequestDispatcher("/redirect.html").forward(request, response);
I have a BIRT report that I've created in GWT and I'd like to save it to the server and then open it back up. I feel like hardcoding the url is the wrong route to go (ie C:/files/foo/foo.html) but I'm not having much luck figuring out another way of doing it. I tried using GWT.getModuleBaseForStaticFiles() but if i use that I get an unsatisfied link error in BIRT. What can I use to save/open a file, what is considered the best practice? Thanks.
edit: bad wording, the BIRT report is generated server-side. I'd like to be able to save it server side and then open it up in a new browser window (using window.open I'd imagine?). Nothing is uploaded from the client side.
If your BIRT report is a static file (Not generated during the runtime of your application) You can create a simple Servlet that read the file and copy the data to the OutputStream of the response. If it's generated at the runtime, the client will request it to the server, the server will generate it and return the URL location of the report. The client can open the report with that URL.
Example:
If your BIRT is located under foo/foo.bar you can map a servlet as /foo/*. After mapping it the user will request the url www.yourselver.com/foo/foo.bar The servlet at the doGet method can read the file and stream the content back to the client.
I'm looking for specific code on how to send a file from the server-side of a GWT application so that the client-side user can save it on his machine. Currently, the app allows the user to upload a file, reads it, and puts certain values from the file into editable text boxes. When the user hits the "save as" button, it collects that edited data, puts it back into the file string, and sends that string to the server, where I want it to be put into a file and pushed back to the user on the client side, so that they can save it to their machine. How exactly do I accomplish that?
Sorry if this seems like an obvious thing, but I'm relatively new to GWT and java in general. Thanks!
I think that you want the way for download a file using content-type from server using GWT.
The easiest way that I have found is create a iFrame :
import com.google.gwt.user.client.ui.NamedFramerdddccvc
...
NamedFrame iframe = new NamedFrame(frameName);
iframe.setVisible(false);
parent.addChild(iframe);
iframe.setUrl(url);
It's important that the url from the server return a page with content type "text/plain" or using the valid requested.
What you can do is create a servlet, that generates the text as content and set the matching mimetype for the content. In you app you can call this servlet via the by Fernando suggested IFrame method.
There are many suggestions here on Stackoverflow on how to do it. Search for [java] file download serlvet and you will find lots of examples/guidelines on how to do this.