Get your CodeRanch badge!*
The moose likes Servlets and the fly likes String Arraylist from Java to Javascript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "String Arraylist from Java to Javascript" Watch "String Arraylist from Java to Javascript" New topic
Author

String Arraylist from Java to Javascript

surya naidu
Ranch Hand

Joined: Jul 26, 2010
Posts: 32
Ho do I access an Arraylist from a servlet in Javascript on a JSP

What is the best possible way.Any suggestions please
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60056
    
  65

You obviously cannot pass the data directly. You'll need to create the JavaScript markup that recreates the data.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
duy nguyen H
Greenhorn

Joined: Aug 30, 2011
Posts: 11
Maybe DWR is the thing you want
naveen vemuri
Greenhorn

Joined: Aug 30, 2011
Posts: 2
here's sample code for DOB...njoy

servlet :

public class ArrayListDemo extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doPost(request,response);
}


public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

ArrayList monthList = new ArrayList();

monthList.add("Jan");
monthList.add("Feb");
monthList.add("Mar");
monthList.add("Apr");
monthList.add("May");
monthList.add("Jun");
monthList.add("July");
monthList.add("Aug");
monthList.add("Sep");
monthList.add("Oct");
monthList.add("Nov");
monthList.add("Dec");

ArrayList dateList = new ArrayList();
int date = 0;
for(date=1;date<=31;date++){
dateList.add(date);
}
ArrayList yearList = new ArrayList();
int year= 0;
for(year=1980;year<=2020;year++){

yearList.add(year);
}

request.setAttribute("year",yearList);
request.setAttribute("date",dateList);
request.setAttribute("month",monthList);

RequestDispatcher rd = request.getRequestDispatcher("date.jsp");
rd.forward(request, response);
}

}


jsp...using jstl

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page isELIgnored="false"%>
<html>
<body>
<center>
DOB :
<select name="datelist">
<c:forEach items="${date}" var="dat">
<option>${dat}</option>
</c:forEach>
</select>

<select name="monthlist">
<c:forEach items="${month}" var="mon">
<option>${mon}</option>
</c:forEach>
</select>

<select name="yearlist">
<c:forEach items="${year}" var="yr">
<option>${yr}</option>
</c:forEach>
</select>
</center>
</body>
</html>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String Arraylist from Java to Javascript
 
Similar Threads
How to convert a Java ArrayList into a Javascript JavaArray, inside a JSP page?
How to set a property's value thats part of a ArrayList using Javascript.
Can I Iterate through an Arraylist inside a javascript funtion?
Is it possible to send arraylist from jsp to javascript(same page)
session problem