Link to home
Start Free TrialLog in
Avatar of WarriorHypnotist
WarriorHypnotistFlag for Jordan

asked on

Javascript while loop with jsp passed value error

Im trying to create a javascirpt function with jsp code , i have the following :-


<script language=javascript>
var d ='<%=session.getAttribute("num")%>';

function Disable()
{
      
      <%
      int xxx = 0;
      while(xxx < 'd')
      {
              out.println("document.form1.notapp" + xxx + ".disabled = true;");
              xxx++;
      }
      %>
}

The value of d is 4 , but when i run the code it gives up to 100 raws as below

 
<script language=javascript>
var d ='4';
 
function Disable()
{

document.form1.notapp0.disabled = true;
document.form1.notapp1.disabled = true;
document.form1.notapp2.disabled = true;
document.form1.notapp3.disabled = true;
document.form1.notapp4.disabled = true;
document.form1.notapp5.disabled = true;
document.form1.notapp6.disabled = true;
document.form1.notapp7.disabled = true;
document.form1.notapp8.disabled = true;
document.form1.notapp9.disabled = true;
document.form1.notapp10.disabled = true;
....
document.form1.notapp90.disabled = true;
document.form1.notapp91.disabled = true;
document.form1.notapp92.disabled = true;
document.form1.notapp93.disabled = true;
document.form1.notapp94.disabled = true;
document.form1.notapp95.disabled = true;
document.form1.notapp96.disabled = true;
document.form1.notapp97.disabled = true;
document.form1.notapp98.disabled = true;
document.form1.notapp99.disabled = true;
 
}

Can someone explain to me what im doing wrong and why the loop is not stopping correctly , thank you.
Avatar of apexpert
apexpert

Please try it now:
<script language=javascript>
var d ='<%=session.getAttribute("num")%>';

function Disable()
{
     
      <%
      int xxx = 0;
      while(xxx < d)
      {
              out.println("document.form1.notapp" + xxx + ".disabled = true;");
              xxx++;
      }
      %>
}
Avatar of Sathish David  Kumar N
u have mention as 'd' as string not numeric !

Avatar of WarriorHypnotist

ASKER

i get this error when i remove the ''
org.apache.jasper.JasperException: Unable to compile class for JSP:

An error occurred at line: 30 in the jsp file: /DepSheet.jsp
d cannot be resolved
27:       <%
28:       int xxx = 0;
29:       
30:       while(xxx < d)
31:       {
32:               out.println("document.form1.notapp" + xxx + ".disabled = true;");
33:               xxx++;


Stacktrace:
      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
why have you given
var d ='<%=session.getAttribute("num")%>';

try with this
var d =<%=session.getAttribute("num")%>;
or do

function Disable()
{
     var intVal = parseInt(d);
      <%
      int xxx = 0;
     
      while(xxx < intVal )
      {
              out.println("document.form1.notapp" + xxx + ".disabled = true;");
              xxx++;
      }
      %>
}
i did what you said gurvinder372 but  it is giving the same error again

An error occurred at line: 26 in the jsp file: /DepSheet.jsp
intVal cannot be resolved
23:       <%
24:       int xxx = 0;
25:       
26:       while(xxx < intVal)
27:       {
28:               out.println("document.form1.notapp" + xxx + ".disabled = true;");
29:               xxx++;


Stacktrace:
      org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:85)
      org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
      org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:415)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:308)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
      org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
      org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
here is is code from the begening of the page

<%@ page import = "java.util.*" %>
<%@ page import = "TimeSheet.*" %>
<%@ page import = "java.sql.*" %>
<%@ page import = "java.text.NumberFormat" %>
<jsp:useBean id="e" scope = "application" class = "TimeSheet.Employees" />


<html>
<head>
 
<title>Time Sheet</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language=javascript>
var jsVar ='<%=session.getAttribute("category")%>';
var d = <%=session.getAttribute("num")%>;
 

 
function Disable()
{
      var intVal = parseInt(d);
      <%
      int xxx = 0;
      
      while(xxx < intVal)
      {
              out.println("document.form1.notapp" + xxx + ".disabled = true;");
              xxx++;
      }
      %>
}
<%
Now i got it.
you are parsing javascript variable in JSP scriplet :). which is not possible



function Disable()
{
      var intVal = parseInt(d);
      for (var=0;var<=<%=session.getAttribute("num")%>;var=var+1)
     {
       document.write("document.form1.notapp" + xxx + ".disabled = true;");
     }
}
sorry, use this one

function Disable()
{
     for (var=0;var<=<%=session.getAttribute("num")%>;var=var+1)
     {
       document.form1.notapp<%=session.getAttribute("num")%>.disabled = true;
     }
}
its not looping ??!

function Disable()
{
     for (var=0;var<=4;var=var+1)
     {
       document.form1.notapp4.disabled = true;
     }
}
ok, try this

function Disable()
{
     for (var=0;var<=4;var=var+1)
     {
       var elementName = notapp + "" + var;
       document.getElementById(elementName).disabled =  true;
     }
}

not working as well , also the 4 value may change according to the session.getAttribute("num")

First of all, I would like to tell you about the cause of printing 100 times, it is because of this javascript.

 while(xxx < 'd')

the 'd' is represent ascii code 100 in numeric so it is equal to

 while(xxx < 100)
SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok since 'd' is returning a value of 4 how can i convert it to make it usable number in the loop ?
Try this one.

function Disable()
{
     for (count=0;count<=<%=session.getAttribute("num")%>;count)
     {
       var elementName = notapp + "" + var;
       document.getElementById(elementName).disabled =  true;
     }
}
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the line

var elementName = notapp + "" + var;

Should be

var elemetName = notapp + "" + count;

Sorry for typo error
this is what i get

<script language=javascript>
var jsVar ='R';
var d = 4;
 
 
function Disable()
{
     for (count=0;count<=4;count++)
     {
       var elementName = notapp + "" + count;
       document.getElementById(elementName).disabled =  true;
     }
}
but it does not printing the 4 lines like it should
document.form1.notapp0.disabled = true;
document.form1.notapp1.disabled = true;
document.form1.notapp2.disabled = true;
document.form1.notapp3.disabled = true;

why do you need to print the lines?
if the requirement is to disable the elements prefixed with 'notapp', then it will be achieved with this also.

ok let me check and i will get back to u
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
its working now , i wanted 2 print the lines to see the code but i think your solution is the better one.
the final funcion is
function Disable()
{
     alert(<%=session.getAttribute("num")%>);
     for (count=0;count<=<%=session.getAttribute("num")%>;count++)
     {
       var elementName = "notapp" + count;
       document.getElementById(elementName).disabled = true;
       
     }
}