wood burning stoves 2.0*
The moose likes JSP and the fly likes Cannot fetch data from arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » JSP
Bookmark "Cannot fetch data from arraylist" Watch "Cannot fetch data from arraylist" New topic
Author

Cannot fetch data from arraylist

Gaurav x Jain
Ranch Hand

Joined: Feb 01, 2011
Posts: 39
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>

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61310
    
  66

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?


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Gaurav x Jain
    Ranch Hand

    Joined: Feb 01, 2011
    Posts: 39
    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

    Joined: Jan 10, 2002
    Posts: 61310
        
      66

    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

    Joined: Feb 01, 2011
    Posts: 39
    Thank you. Now it's working
     
    GeeCON Prague 2014
     
    subject: Cannot fetch data from arraylist