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
Related
I have an HTML form in AEM where I have to attach the files and the same files will be sent to one Rest API via Java Servlet.
I am calling the Java Servlet via Ajax and able to send other String data to Java Servlet but not able to send the file Array which contains the files attached to the HTML Form attachment option while submitting the Form. How can I get the file in Java servlet?
In JS
var myFile [] is what I am sending in a ajax call.
$.ajax({
url: /servletUrl,
type: 'post',
data: {
'myFile': myFile,
},
success: function(response){
}
});
In Java :
Enum paramObject = request.getparameter();
When I put the object in HashMap and try to get the file, its type is coming as String not Object.
I am not sure where I am setting it as String.
The servlet has to be able to process Multipart-Messages.
I do not know AEM, but in Jakarta / Java Enterprise Edition / JEE / J2EE:
you have to specifically add the #Multipart annotation to the servlet.
Now, open your browser, press F12 to go into debug details, and when you trigger the request, the Network tab will display alle the infos that are posted. Look up the name of the parameter, usually it's calles file[]
When handling the request in the servlet, you can use the HttpServletRequest's request.getParts() method to find all parameter parts.
With final Part filePart = request.getPart(pFileParamName); and final InputStream filecontent = filePart.getInputStream(); you will be able to access the data.
And this will probably be very similar in most servlet frameworks.
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[]");
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.
I'm new to servlet and jsp, I want to make a simple application using Java servlets or JSP that get the locations of some places using Google maps and then do some calculations, then display the results on the map in a webpage.
I can request places search using this :
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522%2C151.1957362&radius=500&types=bus_station&sensor=false&key=YOURAPIKEY.
It returns a JSON array, but I don't know how to call this link inside java code.
or how to send the location from java-script to servlets.
Simply I want to apply algorithm written in java and calls data from Google maps also return outputs on map.
You can use URLConnection to call the link from Java code and XMLHTTPRequest to send the location from browser (javascript) to the servlet.
Use HTMLUnit: http://htmlunit.sourceforge.net/
It will simulate browser action so you can post whatever you want and have an appropriate method handle the callback.
WebClient webClient = new WebClient();
webClient.setThrowExceptionOnScriptError(false);
try {
UnexpectedPage page = webClient.getPage(url);
InputStream inputStream = page.getInputStream();
// Read the stream
} catch (IOException e) {
e.printStackTrace();
}
Integrating Maps into Your Java Web Application with Google Maps and Ajax (V2 !!)
https://today.java.net/pub/a/today/2006/10/25/integrating-google-maps-into-web-application.html
I have an applet that calls a URL, on a user action, and passes some parameters. These parameters are executed on a ROR server and then I update a partial, which is on the applet's page, based on the result. However, the partial is not updating.
The console says that the partial is rendered but it is not. Is it because I am calling the URL from the applet and the server is unaware that the applet is in the current session, hence it does not know which session partial to render?
Here is the code of the controller:
def add_point
#comments = Comment.find(:all)
render :update do |page|
page[:comment_text].value = ""
page.replace_html :comments_partial, :partial => 'comments', :collections=>#comments
end
end
EDIT: I use Apache commons http wrapper to send the request:
PostMethod post = new PostMethod("http://localhost:3001/vizs/add_point");
post.addParameter("upload[test]", dataImg);
post.addParameter("upload[user_id]", new Integer(user_id).toString());
post.addParameter("upload[viz_id]", new Integer(viz_id).toString());
I think I need to set additional request variables but I am unsure what to set....
Any suggestion on how to solve this?
Thanks,
Slotishtype
rails is not aware about the applet, and does not know how to work with it, you need to send xml or json or even html:
def add_point
#comments = Comment.find(:all)
render :partial => 'comments', :collections=>#comments, :layout => nil
end
parse it in applet and fill appropriate fields by yourself.