Solved

How  to access array from scriptlet in javascript ?

Posted on 2015-02-15
4
662 Views
Last Modified: 2015-02-16
How to access fruits array from scriptlet in javascript ?


<%
String fruits[]= request.getParameterValues("fruit");
pageContext.setAttribute("fruits", fruits);
if(fruits != null)
{
%>
<h4>I likes fruit/s mostly</h4>
<ul>
<%
for(int i=0; i<fruits.length; i++)
{
%>
<li><%=fruits[i]%></li>
<%
}
%>
</ul>
<%
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
var flag=0;
function checkBoxValidation()
{
for(var i=0; i < document.form1.fruit.length; i++)
{
if(document.form1.fruit[i].checked)
{
	document.form1.fruit[i].value==<%=fruits[i]%>
flag=1;
}
}

if(flag==0)
	{
	alert("select smthing");
	}
	
}
</script>
</head>

Open in new window


Please HELP......
0
Comment
Question by:Pradip Shenolkar
  • 2
4 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 40610992
Currently your fruits array is only being computed server-side. Your using it inside server-side tags to generate HTML.

If you need to use it in JavaScript you need to have it inside a variable client-side serialized as JSON.
To do the serialization I would recommend you Gson.
0
 
LVL 27

Expert Comment

by:rrz
ID: 40611486
This question is not clear. Please post another try at your code.  You reference form1 but there is no form in your code. I am guessing there was more to your code because there is no body tag.
Anyway, to answer your question:
How to access fruits array from scriptlet in javascript ?

Let's assume you have an array on the server and you want to use it at the client.    Please run this code. Be sure to inspect the source code that your browser received to see what the JSP sent in the response.
<html>
<head>
<script>
function validate()
  {
    var fruitArray = new Array();
<%
  String[] fruits = {"apple","peach","pear"};
  for(int i = 0; i < fruits.length; i++)
   {
    out.println("fruitArray[" + i + "]=\"" + fruits[i] + "\";");
   }
%>
  alert(fruitArray);
}
</script>
</head>
<body onload="validate()">
testing
</body>
</html>

Open in new window

0
 

Author Comment

by:Pradip Shenolkar
ID: 40611599
What I want to achieve is after submitting the form to same page the checked checkboxes must retain their state using javascript.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
String fruits[]= request.getParameterValues("fruit");
pageContext.setAttribute("fruits", fruits);
if(fruits != null)
{
%>
<h4>I likes fruit/s mostly</h4>
<ul>
<%
for(int i=0; i<fruits.length; i++)
{
%>
<li><%=fruits[i]%></li>
<%
}
%>
</ul>
<%
}
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
var flag=0;
function checkBoxValidation()
{
for(var i=0; i < document.form1.fruit.length; i++)
{
if(document.form1.fruit[i].checked)
{
flag++;
}
}

if(flag==0)
	{
	alert("select smthing");
	}
	
}
</script>
<title>JSP Multiple Checkbox</title>
</head>
<body>
<form name="form1" onsubmit="checkBoxValidation()">
<h3>Select your favorite Fruits</h3>
<p><input type="checkbox" name="fruit" value="Mango"/>Mango</p>
<p><input type="checkbox" name="fruit" value="Apple"/>Apple</p>
<p><input type="checkbox" name="fruit" value="Grapes"/>Grapes</p>
<p><input type="checkbox" name="fruit" value="Papaya"/>Papaya</p>
<p><input type="checkbox" name="fruit" value="Lychee"/>Lychee</p>
<p><input type="checkbox" name="fruit" value="Pineapple"/>Pineapple</p>
<p><input type="submit" value="submit"/>
</form>

</body>
</html>

Open in new window

0
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 40611697
Here is one way.
<%@ page import="java.util.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
  String checkString = "";
  String fruits[]= request.getParameterValues("fruit");
  if(fruits != null)
  {
    List list = Arrays.asList(fruits);
	if(list.contains("Mango"))checkString += "document.form1.fruit[0].checked=true;";
    if(list.contains("Apple"))checkString += "document.form1.fruit[1].checked=true; ";	
	if(list.contains("Grapes"))checkString += "document.form1.fruit[2].checked=true; ";
	if(list.contains("Papaya"))checkString += "document.form1.fruit[3].checked=true; ";
	if(list.contains("Lychee"))checkString += "document.form1.fruit[4].checked=true; ";
	if(list.contains("Pineapple"))checkString += "document.form1.fruit[5].checked=true; ";		  
    out.print("<h4>I like most fruits</h4><ul>");
    for(int i=0; i<fruits.length; i++)
    {
        out.print("<li>" + fruits[i] + "</li>");
    }
     out.print("</ul>");
  }
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script type="text/javascript">
function check()
{
   <%=checkString%> 
}
var flag=0;
function checkBoxValidation()
{
    for(var i=0; i < document.form1.fruit.length; i++)
    {
        if(document.form1.fruit[i].checked)
        {
            flag++;
        }
    }
    if(flag==0)
	{
	    alert("select something");
	}
}
</script>
<title>JSP Multiple Checkbox</title>
</head>
<body onload="check();">
<form name="form1" onsubmit="checkBoxValidation()">
<h3>Select your favorite Fruits</h3>
<p><input type="checkbox" name="fruit" value="Mango" />Mango</p>
<p><input type="checkbox" name="fruit" value="Apple" />Apple</p>
<p><input type="checkbox" name="fruit" value="Grapes" />Grapes</p>
<p><input type="checkbox" name="fruit" value="Papaya" />Papaya</p>
<p><input type="checkbox" name="fruit" value="Lychee" />Lychee</p>
<p><input type="checkbox" name="fruit" value="Pineapple" />Pineapple</p>
<p><input type="submit" value="submit" />
</form>
</body>
</html>

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

773 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