How to make data from a database accessible from other web applications - java

I have a database from which I want to expose data.
Ideally I would like to be able to just add a URL into some other web page and that URL would then call the correct datum using the web app I use to interact with the database.
Would a web service be the best option?

Looks to me like a perfect job for ODATA:
The Open Data Protocol (OData) is a Web protocol for querying and updating data that provides a way to unlock your data and free it from silos that exist in applications today. OData does this by applying and building upon Web technologies such as HTTP, Atom Publishing Protocol (AtomPub) and JSON to provide access to information from a variety of applications, services, and stores.
See it action (showing query results in a browser is just one way to use ODATA).

A URL-based solution as you describe would only work if:
a) the web app framework you use can resolve the URL automatically as it parses and sends the HTML to the browser, or
b) the browser resolves the URL (e.g. the IMG element)
If the web app framework you use can resolve the URL (or if you can extend it so that it does), then you still need something that listens at that URL and retrieve the correct element from the database.

The approach here depends on whether you are doing Ajax style web pages or simple HTML, where each UI update refreshes the whole page.
The latter, a traditional page by page web site, it probably the simplest thing. For this explore JSP technologies. The idea is that you write what looks like an HTML page, but embed in it references to Java objects (or even Java code). In this case you should read up on simple frameworks such as Struts. The broad-brish idea is that you get this sequence of processing
Request arrives from Broswer, interpret it to figure out what the user wants to see
Some Java code talks to the Database gets data puts it in a Java Object
A JSP is chosen, that JSP picks items from the Java Object we just prepared
The JSP renders HTML which is sent to the Browser
In the case of Ajax, JavaScript in the Browser decides to display some data and calls a service to get it. So here, yes a "Web Service" of some kind is needed. Usually we use REST services, which return a payload in JSON format, effectively the data is transferred as JavaScript.
There are plenty of libraries for creating RESTful Web Services, for example Apache Wink.

Related

Updating a live line graph on a web page with values from java application

I have a java application running on PC X and a web application [javascript+html+bootstrap] on tomcat server on PC Y.
The java application has two variables in a class which are distance and Time which are constantly changing at a frequency. In the web application I need to plot a line graph [speed] of these values on a browser opened at any PC A by a user. It should be something close to what stock market graphs look like
I am a relative novice in web programming though I know a little bit of java. How can I send the values from the java application to my web application?
When I search this I get terms like web services, ajax, json, servlets etc. Its more confusing than helpful.
How should I proceed?
My html page is just containing one panel inside which I want to display the graph.
The answer is ajax, JSON, servlets.
Ajax in your javascript to make the HTTP request to your server from the browser.
JSON as the encoding you will use to return the data.
Servlet in your Java code to respond to the HTTP request and send back the JSON data. You will likely want to use a library to create your JSON data, Gson is a good choice.
I suggest finding a tutorial on java servlets as a starting point and settle in as there is plenty to learn.

Separate frontend and backend in Java - Spring MVC Framework

I'm new at Spring MVC framework i want to learn how to fully separate frontend(html,js etc.) and backend(java).
I'm going to use RESTfull services, play with JSONs.
I think i'm going to build Single Page Application.
Most of tutorials shown in jsp pages which i dont like.
I saw my friends company project(Using Spring MVC) they used Embedded Jetty server and in server configuration they assigned two different paths for frontend and backend paths.
I saw frontend codes there was only html javascripts etc. on the backend side the approach was the same.(Fully Separated !!!)
My question is:'How they pass requests from frontend to backend and get results from backend and update frontend'.
Also they were using Maven backend and frontend defined as modules in the root.
Could you share tutorials so i can understand playing with codes ?
'How they pass requests from frontend to backend and get results from
backend and update frontend'
They probably use HTTP[S] as the transport and JSON as the data representation format. Browsers support AJAX which allows you to make HTTP connections without reloading the page.
Could you share tutorials so i can understand playing with codes ?
No, that's not what this site is for.
Comments:
JSP is still very useful for generating HTML on the server. This is pretty close to necessary if you want Google to crawl your site.
Check out Spring Data REST for a framework for quick REST APIs.
Also check out ExtJS or Dojo for good Single Page App frameworks.

How to create Single Page Application (SPA) in java with hibernate and MySQL

I'm trying to choose best set of tools to create Single Page Applications. I would like to be able to use Java and Hibernate on server side along with MySQL. But what about ajax layer of SPA?
Maybe I have entirely wrong idea about that and Java and Hibernate make no sense in this case? But than how to implement complex server side operations?
It's rather question of UI part. The page asks a server for pages data. Java + hibernate could be used to implement service (e.g. REST service which returns data in JSON format). You can use SpringMVC to implement the service.
AJAX calls the service and process obtained data.
You can use angularJS for creating Single Page Application. this is the best framework i found for SPA development so far and using in most of projects developing which needs SPA.
for sample application refer to github
and detail about angularJS

Send notifications from Java servlet to HTML pages

I have a Java servlet from which I need to send few notifications to the HTML page before I send the final response (which is a PDF file).
Basically I need to keep the user updating on steps I am performing while generating the PDF for them.
I guess, one way could be set various attributes using HttpSession and have them printed using JS. But I don't know how to do that dynamically without loading the page again.
Any idea on how can this be done? I am kind of new to JSP and servlets.
There are two ways:
AJAX polling - you send ajax requests periodically, and the server responds. The page does not get refreshed. Check jQuery for an easy way to make ajax calls
Comet - that's an umbrella term for what you are trying to do. Usually it means keeping an open channel with the server. There are many ways to implement comet (including the option above), but in Java, on the server side, there are two things to check:
Servlet 3.0 asynchronous support
Atmoshphere - chooses the best underlying mechanism provided by the container, which includes the async support above
You should probably look forward to use some implementation of COMET (with js library of choice).
If your html code is allowed to use html5 features, you could also use websockets

GWT Dynamic page generation

I have written a webpage with GWT which contains auto-generated Hyperlinks. These hyperlinks currently dont point to anything, however, I want them to display certain dynamic information based on the name of the hyperlink. For instance if the hyperlink says iPhone, it should open up another URL with dynamic information about the iPhone which I retrieve from my database. I know JSP/Servlets are used to generate dynamic information on webpages, but how can I integrate such functionality into my GWT webpage?
Thanks
Great this certainly helps in giving me an idea on how I can go about my design.
As a follow up though I have a question on how I can access my backend DB. Now I have stored some data in a SQLite DB which I want to be displayed on the webpage. I was able to implement backend access via GWT's RPC, however, it doesnt seem to allow transfer of a ResultSet object returned by a query. How can ResultSets be transferred? In my browsing I have seen a few keywords such as DTO, JPA etc thrown around but I dont quite have a picture on how they will plug in.
How about this:
[CLIENT]: add a ClickHandler to your hyperlinks where you execute the following steps:
[CLIENT]: retrieve token from hyperlink (i.e. iPhone).
[CLIENT]: access the backend (RPC, RequestFactory or normal RequestBuilder) and pass the token (iPhone) to the backend
[SERVER]: On the backend (servlet, python, php, etc) handle the AJAX call from your GWT app and return the dynamic information based on the token.
[CLIENT]: Display the dynamic information returned by the server call (step 3) in a HTMLPanel or SimplePanel

Categories