Calling A Java Library From A Webpage - java

Currently I have an applet which is used to process an input (a url provided by the user) but since this is the only interaction with the user I would rather not force them to download the entire app. Preferably I want no extra downloading at all.
Is it possible to have let the user enter a url into an html form and then send that input to be processed server-side and then the output is sent back to the user?
I have considered making a tiny applet which takes the url as an input and then calls a server-side library to process it. Still not my ideal solution. Would this actually work?
What I really want it a jar to be called when the user enters form data. Please help

Is it possible to have let the user enter a url into an html form and
then send that input to be processed server-side and then the output
is sent back to the user?
Yes absolutely. Use JavaScript in the browser to send this to a Java back-end. This is how something like this is normally done, not via an Applet.

Related

Java Server-Side screen resolution detection

I'm building my own HTTP server in java, but i'm facing with a problem: I would like to build a page dynamically by creating every HTML object at runtime, the question is: how can i determine the screen dimension of the client's browser?
This information is not present in the HTTP header, so I was thinking about writing a "fake" webpage that runs a javascript that tells the server about the screen (it should redirect to something like www.website.com/w:1920,h:1080) but I don't know anything about cookies (that I suppose are essential to store those informations).
Do you think that I should learn somthng about cookies or there's another way?
BTW I'm not using servlets, just Socket, because that's what I know... should I use servlets?
Thanks for your time!
Matteo
Server knows nothing about client's screen until client send this information. Javascript is easiest way to determine screen size:
window.screen.availHeight
window.screen.availWidth
AJAX request can be used to send the information to the server where it can be stored in session data and backed in database for example if the user is logged in or identified somehow. In such case you don't need cookies. However solution with cookies is easier, check how to set them via javascript. But I'm afraid such solution would be a bit of non-standard, if your site is gonna depend on javascript why not to use it extensively and generate all objects on client side, get that lazy computer working and save your server's resources :) Just feed data by sending simplest HTML containing script doing the work.
Servlets? Can be really light-weight and done with minimal knowledge if you have time go for it.

Calling a JavasScript function on a remote server from inside my JavaCode

There's a butt on a website that uses the onClick javascript function to run a JS function called HiddenUsers.show() or HiddenUsers.hide()
In my Java code, I've established an connection to this site using the HttpURLConnection class. I'm able to post login credentials with a BufferedOutputStream and I can get responses with an InputStream. But is it possible to send the command for that makes that button respond as clicked in turn triggering the JS function on the server side, or even better call the JS function itself from within my Java code?
is it possible to send the command for that makes that button respond as clicked in turn triggering the JS function on the server side
Perhaps I'm not fully following, but Javascript runs on the client (eg in the Browser), not on the server. So it doesn't make much sense to send javascript code to a webpage through HttpURLConnection. You can however, look through the webpage source and scripts and figure out what happens when the button is pressed and respond. For instance, if the button is a submit button you can use a HttpURLConnection to send get/post data from the form data associated with that button.
or even better call the JS function itself from within my Java code
You can call javascript code through a javascript ScriptEngine contained with java, but javascript calls that change the DOM of a webpage would be out of context.
There is a junvenile joke in your first sentence.
Outside of that, no, this isn't really possible. I assume you're trying to automate gets and posts to another server from java. It will receive the javascript, HTML etc... but it will not execute the javascript unless you go out of your way to get a javascript engine to execute it, and then what good will it do you to "show" something?
I'm assuming your angle is to acquire the users that are "hidden". The users may already be in the HTML markup somewhere, in which case you'll just want to parse the HTML you get from your response and get them. It is also possible that the ".show()" function gets the user list via an ajax call. If that is the case your best bet will be to figure out what that ajax call is, and process it directly with another HttpURLConnection.

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.

How to display dynamic parameters in an HTML Email using javascript?

I retrieving the Off time of a page and returning offtimeQuery.toString() and retrieving the page title String resultPageTitle = resultPage.getTitle(); using java.
I am sending an email to the content authors of all the pages which have reached off time. How do i display this off time and page name in my HTML email using javascript?
It is near impossible to make an email with JS in it. If you do manage, then half of the email clients will tear it apart to protect to client-side computer.
Instead, I assume that you are using a Linux box for hosting, you could do two things that would work. Use a bash file or equivalent to dynamically create the page and then fire it off at given times, followed immediately by an email that serves the HTML of that page. That is pretty easy.
The other way would be to use a JS file to do the same. This could work through either time-based(HARD) or by you accessing the page through a browser when you wanted it to go. Again, dynamically create the page with JS and then use the system to send the HTML of the page.
Don't use javascript. Almost all HTML email clients will not run any Javascript, because it is a huge security hole.
Instead put the relevant data into the body of the email as you construct it in your Java code. Presumably you have, in your Java code, those bits of data, and you have the HTML content you're sending as an email. Insert the data there, at most basic using String.format(template, data, ...). But if you are going to do anything other than trivial replacement, use a proper HTML templating system.
Don't try to include any javascript with a HTML email. You may be able to find an email client where it works, but won't for most of your clients.
Javascript gets stripped from email messages due to concerns with security. You could force a redirect and perform phishing attacks, and steal other nasty info such as cookies from the domain that the email was sent to if Dynamic emails were made possible with javascript.
If you are really interested in displaying dynamic content, and don't care how, think about creating a server script which returns an image. You could pass a static identifier to the script, and it could return a dynamic result.
See my project in php at https://github.com/TabLand/EmailTracker which generates dynamic images. Only the time string is dynamic. I'd show you the demonstration, but would end up logging your useragent and IP address!

How do I get data from a web form?

As a pet project I want to build a program that translate from English to Hebrew.
The way I want to make the translation itself is using a web site called morfix.
In this site can enter an English word and after pressing the "תרגום" (translate) button
the translation is brought.
My first choice is to write this program in Java, and I tried using htmlunit, but it does not work beacuse i guess the site uses AJAX to bring the results.
In what languge can I implement the part that submit the web form?
How can I integrate Java with that other language?
Thanks.
You can also send your request without form submit: http://milon.morfix.co.il/?q=try
You need a http analyzer to check what's going on when that button is pressed (Try Live HTTp headers for firefox). Then you can use any language to make the web request.

Categories

Resources