Suppose, I’m working with Java servlet, JSP, JSTL/EL or JSF. As far as I know, one of the most general ways to retrieve a value of a Javascript variable to simply say Servlet is to store that value into a hidden field using Javascript and retrieve the value of that hidden field through the request parameters HttpservletRequest.getParameter("someParameter");. Are there any possible ways that might be more better than this.
Suppose, I’m working with Java servlet, JSP, JSTL/EL or JSF. As far as I
Share
Depending on the additional frameworks you use or don’t there are some wrappers, not necessary different ways.
For instance DWR does full javascript->java and back, never have to use a form submit again. Its a pretty solid project though it has been at 3.0b for like 3 years.
From the client you can do an ajax call of course. Depending on what javascript library you use (and please use one) I suggest jquery you can pass any javascript variable. The issue becomes decoding it on the other side.
If you want something a little more standard check out RestEasy a JAXRS implementation that supports javascript serialization. It can do some more advanced mapping between request parameters and java objects.
If you are using servlet and can’t use a 3rd party library you can still make calls via AJAX such as