I need my jQuery arrays on my server side Java - java

I'm working on a project and it requires me to create a JSP, get the arrays from afromentioned JSP and create an excel and a PDF from those arrays while saving those on Dropbox running on my server. I pretty much got everything working, but me being new with JSP and jQuery I really can't figure out an easy way of getting those arrays to Java. I could share my code both for the server and the JSP, but figured they weren't needed as the only part I need help with is sending 3 jQuery arrays to Java and that part is pretty much completely missing.
Thanks in advance and I hope everyone is having a good day!

In client side you need ajax request.
var jsonData=[12,11,13,16,17,18,19];
var ajaxOptions={
url:"myUrl",
type:"POST",
dataType:'json',
data: {MyJSONArray:jsonData},
success:function(data){
// codes....
},
}
$.ajax(ajaxOptions);
and server side you can fetch it using getParameterValues
String[] myJsonData = request.getParameterValues("MyJSONArray[]");

Related

encryption decryption in java and javascript

I am calling a restful web service written in java from java script page and from restful web service i am returning J SON data. how can i encrypt the j son data in java so that no one can see the data using firebug and again i need to decrypt the data in java script page.
Somewhere i read about b son but i couldn't get much info about this.
Is there any way to do this.
Thanks in advance.
i am making ajax call from java script like this...
$.post(url,{cache: false, "_": $.now() },function(){
// code
}, "json");
and from server i am returning json data like this
objectmapper.writeValueAsString("String data");
Regardless of which encryption you use between the client and the server, there must be a point for the client where the data can be read. That is in the browser, which is exposed to javascript, and therefore to Firebug.
Bson is bynary Json (http://bsonspec.org/), and, unless javascript reads and writes the stream by itself (without parsing it into a clear text object), you would fall into the same problem.

Is it possible to use File class (Java) in ajax Request (JavaScript)?

I have a simple question.
I have a piece of code to upload a picture to my server. This code is developped in Javascript. It's a simple Javascript code which append File object (javascript) to my ajax request.
this.addPicture = function(file) {
var frm = new FormData();
frm.append("picture", file);
var xhr = new XMLHttpRequest();
xhr.open("POST", "/myserverurl", true);
xhr.send(frm);
It it possible to use File Object (Java) in this request ?
Thanks a lot
No. Java and Javascript are completely different languages, and you can't use Java APIs in a Web page's Javascript. You might want to look at the HTML FileUpload form control for this purpose.
Basically you wouldn't be able to use java inplace of javascript if that's the question.
You could sent the request as mentioned in your code above and then either handle it as a file object on the server side code in java

restful resource providing html to mobile app

We currently handle the communication to/for backend api* and mobile (ios & android) apps.
For a stupid banner-like thingie, we should provide these apps with some HTML they'll render.
This is how it's done now:
String html ="<html>.....{somePlaceHolder}...</html>";
html = html.replace("{somePlaceHolder}", "We're good");
Gson gson = gsonBuilder.create();
gson.toJson(html);
This looks pretty bad to me. Wrapping html inside json is pretty useless.
I'm sure some templating engine can help us out here.
I was wondering what's the best way to do it.
I suppose I could have some HTML files that I could load up and then have my rest resource produce application/xhtml+xml or text/html this one time instead of json.
Anybody has suggestion on some best practices.
Thanks
*we use jax-rs/jersey
Not sure if I understand the whole problem correctly, but why do you want your RESTful service to return HTML in the first place.
I suppose the RESTful service should return JSON (or XML, for that matter) but the main point is, it should return only the actual data; no HTML at all, especially not HTML wrapped into JSON.
The application that calls the RESTful service should be responsible for the displaying of the data, that is wrapping the data into HTML.

Post JSON Data From Javascript To Java

I'm in need of some desperate help. I've been at this for 4 hours, and I'm getting pretty worn out. :/ Here's my situation:
I have a Javascript application that is making a POST request (using jQuery $.post) to an external site. On the external site I have Apache Camel running with Jetty to expose it to the web. The web services I wrote in Camel expect JSON data for all of the requests. For instance, one request needs an id, so I send it {"id": 10}.
Here's my issue: it doesn't work from Javascript. I have a few different tools that will send post requests for me (like the Poster extension for browsers). If I use Poster and set the body to {"id": 10}, it works just fine. I get that exact string in the service.
But, if I post from Javascript, I get something different. Posting the JSON object will give me the string "id=10" on my service side. (It's OK for this scenario, but I will need actual JSON objects eventually.) If I stringify the JSON object, I get the JSON string, only all of the characters are escaped. (Ex. "%7Bid%33...").
I swear I've tried every method possible for posting the data, but I either get the weird already parsed JSON, or the escaped string (or nothing at all). Is there some way I can have Javascript NOT parse the JSON object and just send it (like my posting tool does)? If not, is there a safe, efficient way to un-escape the JSON string that I get?
I really appreciate any help.
I feel like we need a little bit more information, but take a look at this javascript plugin. It may be your solution: https://github.com/flowersinthesand/jquery-stringifyJSON
Try using jQuery.ajax and setting processData to false (defaults to true):
$.ajax({
url: '/where/to/post',
type: 'POST',
data: {"id": 10},
processData: false
});
Usually, jQuery converts anything in data to query string format like id=10. The processData flag tells jQuery to interpret it literally as a json hashmap.
Posting the JSON object will give me the string "id=10" on my service side.
Javascript does not do your this conversion, so your server does it.
It is likely that your server reacts differently based on the content-type of your POST e.g. application/json vs text/plain or text/html, a common feature of REST based services.
The answers here gave me a few hints, but ultimately, it was a lot of tweaking before it would work correctly. I had to do 3 things:
Add processData: false.
Turn the JSON object into a JSON string. The request wouldn't fire if I left it as an object (even if I changed contentType to application/json).
Change the contentType to text/plain. This sent it as a raw string.
And that's what did the trick. I now get the JSON string I want on the server side.

Porting PHP/jQuery application to Java/Wicket

I'm porting working PHP application to Java/Wicket.
I have a lot of complex, well written jQuery/javaScript which I would like to reuse and not change too much.
Obviously I have to change urls in ajax calls and rewrite the server side scripts from PHP to Java.
I tought this task would be simple but somehow I can't figure out how to write server side that would respond to ajax call.
Simple example:
javascript:
function f(){
jQuery.ajax({
data: 'object_type=1&object_id=2',
url: 'ajax/get_object.php',
timeout: 2000,
type: 'POST',
dataType: 'json',
success: function(r) {
alert(r);
}
});
}
Php file ajax/get_object.php:
// ... create $json_string here
echo $json_string;
I have found AbstractDefaultAjaxBehavior which I probably should use to implement server side of such ajax call, but I'm not really sure how to use it.
I'm not really Java kind of guy so try to explain step by step what sould I do :-)
Have a look at This Ajax Wicket tutorial and search for AjaxEventBehavior.
Do note that Wicket assumes that browsers lacking javascript (Braille readers for the disabled for instance) can return full pages (full page reload in stead of AJAX). If you're doing a job for the government that's usually also a requirement.

Categories