how to send file objects to servlet using json - java

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.

Related

Saving current page as html file using java/jsp/jquery

I have a informative web page in my spring based web application which need to be saved as html/downloaded.
My requirement is to save/ download this opened webpage on click of a button on same page.
I used below code in javascript.
document.execCommand("SaveAs",true,"C:\Saved Content.html");
But this is only working in IE and not in other browsers.
Kindly help on this.
Simply no. JavaScript/Jquery is restricted to perform such operations due to security reasons.
The best way to achieve this, would be, to send a request to the server that would write the new file on the server.
Then from javascript perform a POST request to the server page passing the data you want to write to the new file.

java post and download file

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.

How do I use a GWT RPC to save a file on the client side?

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.

Load custom data from HTTP web page

I need to read some specific data in an HTML web page, from Android.
I wrote class that build the exact HTTP address. Now I can load the page and parse the data I want, but since bandwidth is expensive on smartphones, I would like to load just some custom data off the HTTP web page, to save memory.
Is this possible? How could I do it?
Cheers guys :)
Your alternatives are
Build a Proxy and host it on your server, which will parse the web page, and return relative data to the smart phones.
Read the response stream and once you get all the data you need stop reading. this is depended on how large the page is and where the relevant text you need is located.

GWT applications and the returned response from the server

I have some GWT application that run on the server.
we are subscripting with some solution that pings this application in a regular interval of time.
The point is, this solution (service) checks the returned response from the server to contain some pre-defined keywords.
But as you know, GWT return plain empty HTML page with the data contained in the .js file.
So, the Ping service will not be able to exmain the pre-defined keywords, Is this statement true??
And if this is ture, cannot we find any workaround solution to solve such problem?
Thanks.
The problem you are facing is related to the crawlabitlity of AJAX applications - Google has some pointers for you :) Generally, you need a headless browser on the server to generate the output you'd normally see in the browser, for example see HtmlUnit.
Only the initial container page and the loader script that it embeds are HTML & JS. Afterwards, you use GWT's RPC mechanism to exchange Java objects with the server, or Ajax (eg. RequestBuilder) to exchange any kind of data with the server. you name it: JSON, XML, plain text, etc.

Categories

Resources