I'm trying to assign a js variable to java's request or session and want to access it from the server side. I dont want the js variable to be passed on in the URL. Help me, I'm seeking for a solution.
Thanks in advance
JavaScript only runs on the browser, it has no connection to the database except for ajax type calls.
But, you can add data to the post request for example from javascript, and that would get to the servlet, which is probably your best solution.
But, this really depends on which javascript library you are using, though, for more details.
I'm not sure if I understood your question. But if You want asynchronously manipulate "objects in browser" from server it is not possible directly.
There are workarounds, search for reverse ajax.
It sounds like you need to review the difference between GET and POST variable submission. GET will put it in the address (exmple.com/index.html?var=6) and POST will not. After that you'll have to look at your js library to determine how to direct what the variable hooks up to.
The typical most simple way is to use hidden input fields and have your JavaScript fill those input fields with the values you need. When your form gets submitted the hidden input fields will be accessibel in your Java
Related
I have GET Method. I want to pass something like Student_id in request but without showing it in URL. I know for that we use POST Method. But I dont want to use POST since I am getting someother issues.
If Student_id is something you don't want the user to have, then don't send it. Ever. There's no way to make it safe once it's in a user's hands.
Perhaps you can use a frame as a mask so the domain doesn't change whilst the form submits. You can sneak a REFRESH header in there for 0 seconds with the _top addition, making it nearly impossible for someone to follow your form's weaknesses.
You can't eat the cake and leave it whole.
Your best option is to encrypt the Student_id somehow, you can have your own simple method if it's not very sensitive data, and you can use more complicated algorithms but it can never be 100% safe.
Another option is to not submit the form at all, but AJAX instead to pass the values to server.
You can not do it.
First learn the difference between get and post method.
Get is used to query something with some parameter and post is used to create or update content in the server via http.
It is better using ajax get if you want to hide the parameters form the user.
Look at these link:
http://api.jquery.com/jQuery.get/
http://api.jquery.com/jQuery.ajax/
Cant help to wonder, what does this have anything to do with java.. However, to understand your problem. I would suggest that you use $_SESSSION or setcookie(); So before sending the user to another page, you would first save the user_id in some form.
This question is related to a previous question
Passing Variable from page to page using ASP.NET (C#) without using QueryString
The difference in my case is that the request is coming from a different website (in java) to my website (in asp.net). I do not want the variable to appear in url.
Any suggestions !!
To explain my scenario, we are making a webpage(plugin), which can be called from any other website. To authenticate request, i am looking for a mechanism when other website will pass id & auth-key to my page. This i can use to authenticate the request. I do not want these variable to be visible.
A POST operation would work. The variable would still be part of the request, but it would not be readily visible to the user. I say "readily" visible because it won't be part of the requested URL, but it would be visible if they were to use a tool like Firebug. Short of sharing a database or some other form of "out-of-band" communication, I'm not sure it can be done any other way...
Well as chris mentioned doing a POST is the best way to achieve this. Else you can look at using javascript to achieve the same. Its pretty easy to use JS libraries to achieve the same.
Some of them that come to my mind are
a) Jquery
b) YUI
c) EXT (now Sencha i guess)
But I would definitely recommend jquery.
With jquery you have apis to do post operations. here is more on how to achieve the same.
http://api.jquery.com/jQuery.post/
Hope that helps.
I don't think it can be done without a query string. I know sessions won't work because sessions cannot be shared between Java, Asp, Asp.net, Php etc..., at least not nativly. If you have a database where you store the sessions, you can always use a session id in a query string and therefore simulate cross-language-sessions.
I am using wicket application to initiate my Java method in my code. I have the url as follows
http://localhost:8080/web/resources/org.apache.wicket.Application/StartMethod
I want to add an extra option at the end client=cutomser1
http://localhost:8080/web/resources/org.apache.wicket.Application/StartMethod?client=customer1
As I want these use the same code but for different customers
So when I run this URL from command promte it should fetch the Customer1 into my Java code
in this way..
String client = customer1
How can I implement this in Java. I mean I want to fetch this String name customer1
Wicket is strong in data binding and you should take advantage of that.
If you display in the browser data from Customer1, the Wicket knows that when you do requests from that page.
The translation of request parameters to variables happens behind the scene.
That is disconcerting at first but really powerful when used as intended.
I would suggest you select a good tutorial from the answer to this question and go through a couple of examples to see how things are done. This may take an afternoon but this will pay back for itself before the week is out.
I think I am a bit too late, but still I will say that you can use PageParameters in your page and you should make the page bookmarkable.
I am interested in creating a simple web application that will take in user input, convert it to an XML file and send the file to a database.
Coding wise I feel I am okay, it is just the general setup and what implementation to use I am a bit unsure of.
At the moment I have a JSP page containing a form, the user fills out the form and on submit a POST method is sent to a servlet, in the servlet doPost() method the servlet is instantiating a java object and passing it the user inputted data. The java object then writes that data to an XML file and sends it to the database via REST.
All I would be interested to know is if this the standard/optimal way of creating such a web application.
Any and all feedback is appreciated.
Thanks
For a "simple webapplication" this high level approach looks fine in general. However, if you want more critical feedback, you'd need to give more details about the low-level approach. It may for example happen that it isn't memory efficient and thus may break when the webapp is been used by over 10 users concurrently, just to give an example.
I only question the choice for the GET method. You'd normally only use it to retrieve data (SELECT), not to create/alter data (INSERT/UPDATE/DELETE). For that you'd normally use POST, so that no one can execute it "accidently" by just clicking a (bookmarked) link. Changing GET to POST isn't that hard, add method="post" to the <form> element and rename doGet() to doPost().
As title says, how do I call a java class method from a jsp, when certain element is clicked (by example an anchor)? (Without reloading the page)
If this can be done, how I pass the method, some part of the html code of the page that invokes it?
Im using jsp, servlets, javascript, struts2 and java, over Jboss AS.
What you want to do is have javascript fire off an AJAX request when the said element is clicked. This AJAX request will go to the server which can then invoke any java code you want.
Now you can build this all yourself or you could use one of the many off the shelf solutions. I would recommend Googling around for a JSP Ajax tag library. Like this one http://ajaxtags.sourceforge.net/ .
As Marko pointed out, you might need to read some more about the client/server separation in web programming. If you want a framework to help you do remote Java invocation from Javascript, have a look at DWR.
You can use a Ajax call to do it .
Now when the HTML object is clicked call a java-script. Then in the JavaScript make a Ajax call to the servlet something like this
$.get("Query?ID="+id ,function(RespValue)
{
}
Here Query is my servlet mapping defined in web.xml and Id is the parameter i am passing you can sent multiple parameters too. and RespValue is the response value returned from the servlet.
In the servelt write a do Get method and execute your java code. If you want to return some value use the function(RespValue) else remove it.
You can't execute server side java code in client browser.
What you can do is to perform new http request that will perform some action on server and return the action result.
Given the tone of the question better go read some JSP tutorial. No forum post answer will explain it better.