File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes Jsp & JavaScript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Reply locked New topic

Jsp & JavaScript

kanth varada

Joined: Feb 14, 2006
Posts: 14
Hi Friends,

I declare a variable in Jsp

<%! int x=0; %>

and i declare one more variable in JavaScript.

var i=1;

what i want can assign the javascript variable value to jsp variable.

that is how can assign 'i' value to 'x'......

Pls help me friends.........
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

I thought that we'd already talked about his
You cannot.

[My Blog]
All roads lead to JavaRanch
Shilpa Asuthkar

Joined: Dec 22, 2005
Posts: 10
there is one solution to ur problem.....
store the javascript variable in a form field and then set it to the variable of in the jsp.... this form is a dummy form which on submit goes to javascript and the back to the same form
for example.......

<script language="javascript">
function meth()
var i=0;
<%! int x=0; %>
<form name="myform" action="temp1.jsp" onSubmit="return meth()">
<input type="text" name="t1" value='<%=request.getParameter("t1") %>' >
<input type="submit" value="submit" >

<% if(request.getParameter("t1")!=null) x=Integer.parseInt(request.getParameter("t1")); %>
<%= x%>

tough we r not supposed to use scripting in jsp but thats how u want want to set the value of x.....
hope this will help u.......
let me know...

kanth varada

Joined: Feb 14, 2006
Posts: 14
Hi Shilpa Asuthkar ,

thanks for sending me the stuff..but when iam displaying the t1 value

<%=request.getParameter("t1")%> it is displaying null every time..that means it is not storing the i value in


the steps what i wrote is.....

<script language="javascript">
var i;

if(some logic)
<input type="hidden" value=<%=request.getParameter("desc") %> name="desc">

-----------------------It is displaying Null.....

pls let me know the mistakes iam doing.......
I agree. Here's the link:
subject: Jsp & JavaScript
It's not a secret anymore!