How to pass data from java backend to html page? - java

in my backend i use java and i need to display google chart in html page. im using a tool to generate a url by retrieving the data, do some calculation and create a url to retrieve an image for this chart. my question is how could i pass this url to the html page (portlet) from java to this page.

you would use ajax to invoke the server and request the url. The server does its thing and sends the url back in the response. You would then use javascript to update the dom (i.e. html) on the page.
I recommend using a framework like jquery to make the ajax request and update the DOM. Plenty of examples are online.

use servlet or JSP you can set the response to send values you want.
AJAX use httpRequestXML to send request and can have a callback function to have response

Related

Knowing about page which has called RESTful API

How can we know the name of the page or service or state of page which has called RESTful API in java file. Something like $state.current.name in AngularJS.
I have created api's in java using #RequestMapping(). In this java file I want to know about the name of page that has called this API. As I want to call same api on different pages and want to log different strings according to the page which has requested the API. I am using $http.get(api) in AngularJS to call API.
We found the most reliable way to achieve knowledge of the current URL/state is to send it as part of the request payload. This gets around issues of HTTP referer header stripping. You already have $state.current.name, so adding this to your API-calling JS service function should be straightforward.
If you need to add this to every request, one option is to use an interceptor to add the metadata into every HTTP request which contains JSON.

How to connect webservice to web interface?

Assume we are designing a Spring webservice. We need to build also a web interface for it. As i understand, browser needs an HTML document to display it to user.
Can HTML be created on server and be sent as response to client's browser or only xml docs can be a response? Any details would be helpful.
If we need to process an XML response, build HTML and display it in browser, what is the most convenient way in Java context?
Best way you can make Spring boot restful web service and get json output from it and display it with Angular2. or you can use Spring Mvc and create web template with jsp pages and you can display it on browser.
here is the best video for follow spring boot
1 - You can have static HTML on the server that is served as user requests your webpage.
2 - Add some javascript functions on the HTML pages to request your Spring java aplication endpoints and return response as json (or xml). For this purpouse Angular could be a good choice as stated above.

Struts2 <s:token> with multiple AJAX Call using jquery

I need to make few pages CSRF protected in my project. I am using struts2.
I have a page where I make multiple JQuery AJAX calls at the same time to get data from server.
My problem is that if I pick token parameter from JSP and send it with one of the JQuery AJAX
calls then what about other AJAX Calls because once the token is sent to server that token will be invalidated after use.
Any help on this is appreciated.

Send HTTP POST request in Spring MVC

I am working on a project that uses Spring MVC, and one of the requirement involves sending request to an external services to handle some transactions.
The problem now is the external services only takes POST method, and I've looked everywhere and could not find a way to forward user to external site as a POST request(similar to form submit).
What we are trying to do:
1) My Controller will receives request and execute some backend thing and construct some parameters to pass on.
2) *Send request to external service via HTTP POST method with some parameters.
(note. User's browser will show URL of the external site.)
I've looked into different return types for Spring MVC and could not find anything that fits.
Any advice would be greatly appreciated.
Thanks!
You can use the Apache HttpComponents library to send HTTP requests from you controller. It's easy to use, and there's plenty of documentation and examples.
Supposed that this is not an issue related the Spring MVC. That's the same solution Which you try to implement such operations in Servlet I think.
First of all, this external site should be an async service and you neednt get the response from it, right? If yes, i think the simplest solution is that you can new URLConnection within your Servlet or Controller, construct the params and set the POST method, and then send out the request. After that, you can response to your local page as you like.
The second solution, a dummy JSP page is needed. The page will post a form to external service and redirect to the target page as you needed. No matter you use a javascript or not, I think the user will see a refresh on the browser.
You won't be able to redirect as that implies a GET.
Since your client is using a web browser, it should be possible to return an HTML document with a form, and then to submit that form (with a method attribute of POST) when the page has loaded.

How to display results on same page as requesting page in jsp-servlets

I developing this web app using jsp and servlets. I was wondering how to display results on same page as on from which the request was made. For example, usually what I do is that a request is sent from one jsp to a servlet and then servlet sends the response/results on separate jsp. I am want to show a database results on same page. How can this be done?..A code snippet would be appreciated. Thanks
Just set the necessary data in the request scope and use RequestDispatcher#forward() to forward the control to the desired JSP which in turn can generate the appropriate HTML based on the results.
request.setAttribute("results", results);
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
This is also demonstrated in the hello world examples in our servlets wiki page.
That's totally posible, but you need to include JavaScript in your toolset. What I'm suggesting is an AJAX approach in your application, and that implies intensive use of client-side scripting (i.e JavaScript).
This tutorial from DeveloperWorks makes a pure JavaScript/Servlet approach. If you want to do more advanced stuff, I strongly suggest you to use a JavaScript Framework with AJAX support, like JQuery.
From your servlet, send a redirect to your same page. Also, set some attributes in request and show it in your page, e.g. save List<MyObject> in request, then in your jsp read the list from request and show the values in a table.

Categories