Solved

How  to access array from scriptlet in javascript ?

Posted on 2015-02-15
4
596 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now