I have a situation where in I write to a text file programmatically using java and simultaneously I read from the same file using jQuery.
The problem I face is jQuery is unable to find the updated content whenever a content is written into the text file via java.
I have Googled a lot but the only results I find are for java and java processing and not for java and javascript (i.e A Client side and Server side)
I am not sure if this is even possible.
More about the question:
I write into the file the crawling results using java and I am trying to display the same using javascript (jQuery.post() method).
JAVA
A multi-threaded crawling program that crawls a website and does some functionality. I am trying to write some content into a text file using the same java program as and when the crawling happens. The content I write mostly are the details about which thread is getting invoked and what is the current link that is being crawled.
The reason I write this in the text file is I need to show the output in the UI so that people looking at the UI will understand what happens.
Writing happens perfectly as expected.
JAVASCRIPT (jQUERY)
This using the
jQuery.get or post ("sample.txt", function (result) {
$("#someID").html(result);
});
It reads from the text file normally but when java and javascript both are trying to access the file, It is the java that dominates leaving javascript behind thus jQuery is unable to fetch the updated content as and when it happens.
I guess this explanation is more than sufficient to make people understand what exactly my problem is !
On the whole, java and javascript try to access the same file at the same time. So there comes this issue.
Any help is appreciated.
Thanks in advance
I think the file is cached. Easiest thing is to request the file by different urls. Try something like "sample.txt?rnd="+Math.rand()
There can be synchronization problems and your data will be corrupted.
I have a question, is it must be done with Ajax? I think you are trying to figure out about
Ajax push and pull
This is not very easy to do and I wouldn't really recommend it. However, there is a better technology called websocket. So what you can do is, client can submit request to the server to write data into a file then server can send back updated content to the client. Moreover, this is much better than achieving the same objective through numerous amount of HTTP requests.
Additionally, if you want the crossbrowser compatibility, have a look at http://socket.io/
Thanks for all those who were trying to help me out.
I have finally come up with a solution. I, instead of using jquery post to directly read from file, am using another jsp file that reads the file contents and prints using out.println on screen, and after which I am using jQuery post to get the content written by that jsp file. Hence the synchronization problem is avoided.
Here is more about my explanation:
Earlier I had
java program -> Text File <- javascript (jQuery post) // Resulted in synchronization problem where in javascript was not able to access the updated content.
Now
java program -> Text file <- JSP file <- javascript (jQuery post) // Avoided the synchronization problem as that file is accessed by the same server side language. After that jQuery reads the content printed by JSP page.
After many changes, finally came up with one good working solution.
Thanks all.
Related
I've been trying to do this in a liferay module project. By making an input type=file in my view.jsp and saving it in a java File variable but i can't get it to work. Is this possible? and if yes, how?
So far i've looked for a paramutil method that i can use but there don't seem to be any. And i've tried to use request.getParameter but this doesn't allow for the type File to be used. After that I tried to create a upload request and fill it with the data from the form. But this also didn't work for me.
I wanted to make a form in my jsp file and let people upload a file via the input and let my java code run on submit to add the file to the documentlibrary.
A java File (as in java.io.File) is an abstraction for a pointer to something sitting on your file system. When you upload data in a portlet or any other means in a web application, you're first dealing with a stream of data that usually has no representation on the hard drive (unless you explicitly store it there). Thus, ParamUtil will not reveal anything of type File, because that's - by definition - not part of a http request.
There is a lot of upload sample code, that you might need to adapt to the version you're running (for example this) - but you'll first need to understand that you're not looking for any representation of data on your hard drive.
in my project I need to read some web pages. Usually it is pretty easy: I read the source code using java classes, parse the output and save interesting data.
But sometimes it is harder; for example reading Google pages. I think it is because of javascript. Do you know to get the real web page code, I mean without javascript? For example if I analyse the page using the Firebug extension of Firefox I read exactly what I need: javascript is correctly replaced by its results. Any idea to do it using Java?
Thanks in advance
I need to convert a web page [which has not public access] to PDF or Image [preferably to PNG].
Web page contains set of charts and image. Most of the charts are populated through Ajax calls so there is a delay between page load and chart load.
I am looking answer for any of these questions:
1- I found set of snapshot api's but none of them support accessing my internal page. Since the web page I am trying to export is not public I need to be authenticated. Biggest problem is I cannot send request headers [such as session-id, cookie or other variables] along with these API's. It seems they don't support this kind of functionality.
2- I am not sure if I can do following: Login to my web page with HTTP client, add http headers, send get call and get HTML string. Then use one of the converters to convert it to PDF. What I am not sure is if it's possible to get proper PDF from the HTML string I got from http client since resources [css, js and etc] will be missing. I want my pdf/image looks exactly as it on the web site.
I really appreciate if you can help.
Thanks in advance,
ED
You're probably best of using wkhtmltopdf, which is a server-side tool and is easily installed.
There are two parameters you can use to wait for your Ajax to finish, try:
javascript-delay to influence the time the program waits for the JavaScript to finish
window-status to wait for a certain return code for the window
See the extensive manual for this program here
wkhtmltopdf generates a PDF and wkhtmltoimg generates an image, which is PNG (as you requested) by default.
Authentication is difficult because it involves security. Because the operation you are describing is unusual it is likely to result in all kinds of alarm bells going off. It is entirely possible to do but it is fraught, easy to get wrong and fragile in the face of security updates and code changes.
As such I'm going to suggest an alternate method which is one we often recommend for ABCpdf (on which I work). Yes we support standard authentication methods but the beauty of this approach is that it is robust and is applicable to other solutions (eg Java based) and novel authentication methods.
Typically you just want a PDF of the current page. The easiest way to do this is snaffle the HTML. The way you do this rather depends on your environment. For example under ASP.NET you can obtain the HTML of the current page using the HttpResponse.Filter property or by overriding the Render method of the page. The way you do it will depend on what you're coding in.
Then you need to save this HTML to a file and present it to your solution via a 'file://' protocol URL. Now obviously at this point any relative links will be broken but this is easily fixed by dropping in a BASE tag that references the place they are located.
Generally the types of resources referenced by an server-side page are static. So if you can create a tag that references the actual files rather than a web site, you will bypass any authentication for access to these resources.
That still leaves the AJAX based problems which are another can of worms. The render delay method is something we have supported for many years (from before AJAX was around) however it is not terribly reliable because you just don't know how long to wait.
Much better is a tighter link into the JavaScript via a callback you can use to determine if the page is loaded. I don't think ABCpdf is going to be appropriate for you since it is .NET but I would certainly encourage you to look for a Java based solution that uses this type of more sophisticated approach.
I don't have the code available right now, but I'll describe my situation and post code later on :). Basically, my problem is with uploading image to server via POST (from Java desktop application / client). I found some discussions here on this matter, but it either included using org.apache.commons.httpclient classes, and I have most of the code implemented using URLConnection class, OR with bugs in it. Check this selected answer to see what I followed in as much detail as possible: Using java.net.URLConnection to fire and handle HTTP requests
Anyways, I created php script on my local server that handles form with file upload. I checked it from one static html that was created along with .php. I then checked it without file upload, and it works ok.
It also works ok when I send plain text file. This looks like a problem related to encoding. Maybe some strange character is breaking my transfer?
As I said, I'll include code if necessary later today, but I'm interested in your opinions / experience related to encoding images when sending via POST. Should I do something else, that's not being mentioned in the link above.
QUESTION: do I need to use some kind of encoding to actual data when sending binary file in multipart message via http post?
Sorry for no code right now.
Cheers
As for your question: yes, you need to encode the file.
I would use HttpClient for this as it takes care of all those things for you. No need to reinvent the wheel. See How to upload a file using Java HttpClient library working with PHP for code.
If you are using URLConnection to handle multipart/form-data manually, I guess you you need to add "Content-Transfer-Encoding: binary" to the image part. See W3C's docs.
i am very new to jsp... i am currently doing a project where i have to interface a card reader with my html page.
i got the card-reader code in a cpp and .h file. is there any way i can use these file with my jsp.. or do i have to recode it in java and include a .js file.
specifically, i have a text input for ID on my page. i need it to be populated with the input from card. i got the code to interact with card and extract that number in cpp program. so can i like call that function from my html page?
Why on earth you need to interface your card-reader to your JSP page. It doesn't make any sense to me, I am sorry. First understand that JSP is a Java web technology for presentation, which runs on server and spit HTML to the browser. Hence, what you get on the client is HTML.
Now, could you please elaborate what you are trying to achieve?
There are several way to do this:
You could do a system call from your jsp if your C++-code can run standalone.
You could use a Java-C++-bridge.
You could use the Java Native Interface.
You will have to look into the Java Native Interface if you want to reference C++ code from java.
For more information see the following:
Wikipedia
API Guide
Nice Guide in PDF format
A jsp renders HTML, in the part you will see in your browser you are no longer in your jsp, you are not even in your code anymore.
If you want to read a card from an HTML page you will need to ignore the fact you have jsp technology and realise its HTML technology you are using.
SO you will need an applet, some flash, some activeX or other browser technonlogy first before even trying to interface with the cpp
if you need to read from card JSP cannot help you. If you read card number otherwise and send it to JSP with POST, then you do not need any reading. What you might need is signed applet on user's side which will try to read card from card reader. Then I will advise you to use javax.card - java 1.6 has a support for reading smart cards ...
http://java.sun.com/javacard/