• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Cannot fetch data from arraylist  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to fetch names of friends having same hobby and My problem is I cannot set object of listbox in arraylist and cannot retrive names from list Please help me.

This is my HTML page
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Parameter in JSP </title>
</head>
<body>
<form method="post" action="TestJSP5.jsp">
<select name="hobby" size="1">
<option>Horse Riding</option>
<option>Boating</option>
<option>Extreme Knitting</option>
<option>Speed Dating</option>
</select>
<select name="names" size="5" multiple="multiple" >
<option value ="ravi"> ravi</option>
<option value="Akash">Akash</option>
<option value="vab">Vab</option>
<option value="gustav">gustav</option>
</select>
<br>
<br>
<center>
<Input type="Submit" value="Enter">
</center>
</form> "
Parameter in JSP through HTML
</body>
</html>

This is my JSP Page

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="java.util.*" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Parameter</title>
</head>
<body>
The friends who share the hobby
<%=request.getParameter("hobby")%>
are: <br>
<%List al=new ArrayList(); %>
<%al.add(0); %>
<%al.add(1); %>
<%al.add(2); %>
<%al.add(3); %>

<%request.setAttribute("names",al); %>

<% al=(ArrayList)request.getAttribute("names");%>
<%if (al!=null && al.size()>0){ %>
<%Iterator it=al.iterator(); %>
<%while(it.hasNext()){ %>
<%=it.next() %>
<%}} %>
</body>
</html>

 
Author and ninkuma
Marshal
Posts: 66823
168
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few points:
  • Java and scriptlets in a JSP is a bad bad practice. You should strive to stop doing that.
  • You should submit forms to servlets, not to JSPs. That will also make fetching the data easier.
  • You haven't really told us what's happening, just that "I can't do it". That doesn't help us help you.


  • So what's really the problem? What have you debugged so far?
     
    Gaurav x Jain
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry for the inconvience. In this jsp page There are 1 combobox and 1 listboxe combobox contains Hobbies and listbox contains name of friends. I just want to select a hobby then select multiple names from friend list and display it to web page output must like

    The friends who share the hobby Boating are:
    ravi
    vab
     
    Bear Bibeault
    Author and ninkuma
    Marshal
    Posts: 66823
    168
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Then why bother with a list at all? If you are going to continue to submit to a JSP rathe than a servlet (bad practice) then the submitted parameters are available via the param built-in scoped variable which you can access with the EL.
     
    Gaurav x Jain
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you. Now it's working
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!