?
Solved

Javascript while loop with jsp passed value error

Posted on 2009-12-21
24
Medium Priority
?
455 Views
Last Modified: 2012-05-08
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.
0
Comment
Question by:WarriorHypnotist
  • 10
  • 7
  • 5
  • +2
24 Comments
 
LVL 4

Expert Comment

by:apexpert
ID: 26094904
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++;
      }
      %>
}
0
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 26094913
u have mention as 'd' as string not numeric !

0
 

Author Comment

by:WarriorHypnotist
ID: 26094931
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)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26094937
why have you given
var d ='<%=session.getAttribute("num")%>';

try with this
var d =<%=session.getAttribute("num")%>;
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26094944
or do

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

Author Comment

by:WarriorHypnotist
ID: 26094973
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)
0
 

Author Comment

by:WarriorHypnotist
ID: 26094975
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++;
      }
      %>
}
<%
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26095009
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;");
     }
}
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26095035
sorry, use this one

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

Author Comment

by:WarriorHypnotist
ID: 26095148
its not looping ??!

function Disable()
{
     for (var=0;var<=4;var=var+1)
     {
       document.form1.notapp4.disabled = true;
     }
}
0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26095167
ok, try this

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

0
 

Author Comment

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

0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 26095271
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)
0
 
LVL 40

Assisted Solution

by:Gurvinder Pal Singh
Gurvinder Pal Singh earned 164 total points
ID: 26095282
"not working as well , also the 4 value may change according to the session.getAttribute("num")"

I know sir.

you should have tried that yourself

function Disable()
{
     for (var=0;var<=<%=session.getAttribute("num")%>;var=var+1)
     {
       var elementName = notapp + "" + var;
       document.getElementById(elementName).disabled =  true;
     }
}
0
 

Author Comment

by:WarriorHypnotist
ID: 26095293
ok since 'd' is returning a value of 4 how can i convert it to make it usable number in the loop ?
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 26095338
Try this one.

function Disable()
{
     for (count=0;count<=<%=session.getAttribute("num")%>;count)
     {
       var elementName = notapp + "" + var;
       document.getElementById(elementName).disabled =  true;
     }
}
0
 
LVL 11

Accepted Solution

by:
sompol_kiatkamolchai earned 336 total points
ID: 26095342
sorry. for get count++.

function Disable()
{
     for (count=0;count<=<%=session.getAttribute("num")%>;count++)
     {
       var elementName = notapp + "" + var;
       document.getElementById(elementName).disabled =  true;
     }
}
0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
ID: 26095410
the line

var elementName = notapp + "" + var;

Should be

var elemetName = notapp + "" + count;

Sorry for typo error
0
 

Author Comment

by:WarriorHypnotist
ID: 26095469
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;

0
 
LVL 40

Expert Comment

by:Gurvinder Pal Singh
ID: 26095581
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.

0
 

Author Comment

by:WarriorHypnotist
ID: 26095623
ok let me check and i will get back to u
0
 
LVL 11

Assisted Solution

by:sompol_kiatkamolchai
sompol_kiatkamolchai earned 336 total points
ID: 26095652
it should be

var elementName = "notapp" + count;

Hope this help,
Sompol
0
 

Author Comment

by:WarriorHypnotist
ID: 26095694
its working now , i wanted 2 print the lines to see the code but i think your solution is the better one.
0
 

Author Comment

by:WarriorHypnotist
ID: 26095725
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;
       
     }
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Social messanging services like WhatsApp and Facebook can help businesses in ways that many owners don't even imagine, giving new opportunities to connect with customers. Discover some of the most innovative things they can do for your company.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question