Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String Arraylist from Java to Javascript

 
surya naidu
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64610
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You obviously cannot pass the data directly. You'll need to create the JavaScript markup that recreates the data.
 
duy nguyen H
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe DWR is the thing you want
 
naveen vemuri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic