Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to retrieve javaScript variable into jsp

 
Prexa Shah
Greenhorn
Posts: 9
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone tell me how can i retrieve javaScript variable into jsp...

i've tried using hidden field bt its not working...

have a look at code:

this is what i'm doing in javaScript:


and in jsp page:



i'm getting NumberFormatException....


Thanks in advance.
Have a nice day...
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the jsp page is called on submit of any other page? Or else how could you expect the following

to work?
 
Ankit Tripathi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to javaranch,Prexa...

You can't retrieve a javascript variable directly from your java code in a JSP.Because java code in your JSP is going to be executed before HTML is going to be interpreted with javascript at your web browser
However to access client-side data on the server, you have to send it from the client to the server,which can be done by using Ajex or jQuery.

document.getElementById("start").value=start;
document.getElementById("limit").value=limit;

But according to your example you are trying to access HTML form fields in your javascript(these might not be called as java variables(session,request..etc) because their values are interpreted at server side not at client side ).

int start = Integer.parseInt(request.getParameter("start"));


"start" is a parameter which should be received directly from HTML Page/JSP/HTML in servlet submitted just before.from where "start" is coming?
 
Prexa Shah
Greenhorn
Posts: 9
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did some modification and its working....


in javaScript:

document.location.href ="Result.jsp?start="+start;

and in jsp:

try{
start=Integer.parseInt(request.getParameter("start"));
out.print("start-->"+start);
}
catch(Exception e)
{
e.printStackTrace();
}
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is the thing, the page is getting submitted to Result.jsp with querystring start=valueofstart, and now request.getParameter("start") will work.
 
Prexa Shah
Greenhorn
Posts: 9
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But here there is one problem the page that i'm passing is fix....



and actually i'm using ajax so main page is not Result.jsp....
so whenever i run my project it redirects my main page to result.jsp...is it possible to pass value in that way my main page remains as it is???

the path of ajax is like this:
admin calls search.jsp using ajax and search.jsp calls result.jsp using ajax
in short admin-->search-->result.

Hope you got it....



can i use this type of code?


 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A bit confusing. Because as far as my knowledge goes, location.href is not an ajax call, correct me if I am wrong?
 
Prexa Shah
Greenhorn
Posts: 9
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya it is not ajax call but page on which i'm sending reference is printed using ajax....like this:

in jsp of search page:


which was called from this javascript:



and same procedure is there for admin to search...
sorry if i'm not able to explain properly..
 
Rajani Gummadi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prexa,

As far as I understood your problem (I'm not familiar with ajax, so my question/answer does not refer using it), you want to send some parameters that are being set in a javascript from admin.jsp to result.jsp through search.jsp. Irrespective of whether you use ajax or not, if you are using redirect, the unless you set the value again in search.jsp before sent to result.jsp, the request parameters that are set in admin.jsp would not be preserved through out the navigation (because of redirection - HTTP 3xx). So, if you are employing HTTP redirect, then you need to explicitly set the value in request before sent to result.jsp. But if you are using forward, then its a different case, in that the values which is set in admin.jsp would be preserved through out the path.

I'm not sure, if this is what you are looking for, but its just my thought on this problem.

Thanks
Rajani
 
Prexa Shah
Greenhorn
Posts: 9
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply Rajani...
can you please give me one sample code if possible....because i'm having a little bit trouble understanding your explanation.

Thanks in advance.
-Prexa.
 
Rajani Gummadi
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure, if the below solution works for your case, but if you need to have a variable being set in javascript to be available across your navigation, another approach would be to set in session, instead of relying on request scope. This way, whether you use redirect or forward, as long as you are in that session, the value should be accessible.

What I meant here is, suppose

// The code below is just a flow and requires some syntax fixes and optimization
admin.jsp


search.jsp


What you are doing here is extracting the request parameter and assigning it as an attribute of a session. After this, if you do either redirect or forward, the value should be available, though out the session and if you keep it in application scope, it is accessible through the application . But if you chose to set this in request scope, and need to have the value available through out the navigation for HTTP redirects, you need to explicitly set to request scope before each invocation.

Though I have used jsp for page navigation control, it should have been implemented ideally by some sort of page controllers (servlets), but the idea remains the same.
 
Prexa Shah
Greenhorn
Posts: 9
Eclipse IDE Java jQuery
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply Rajani...

Your suggestion was good i tried to use it but i'm not very familiar with custom tags so may be i'm doing some mistakes..
But as much as i learned about custom tags until now,i think even if i use your code my problem will remain as it is.Because here the problem is not about getting variable,it was already happening in my previous code...problem is that i'm using AJAX and therefore whenever i run my program after this line



the page is forwarded to the Result.jsp which is not i wanted because i'm trying to make next previous(pagination) functionality. And in that i'm using three AJAX operation. The page on which i want to do pagination is Result.jsp. And so instead of redirecting i need only variable on my Result.jsp page(without loosing my search result).

I know its complicated for me and for you guys to understand but i'm really stuck in this from a very long time please help....
anyone who knows AJAX as well as j2EE can help me.....

Thanks in advance.
-Prexa
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic