jsp problem - ArrayList or Vector

hey, im new to java servlets and jsp - im doing some school project (web application) with java servlets and i got stuck - i need to send some data through session set/get Attribute, i made ArrayList but dont know how to initizalise Array list within jsp. i have following Arraylist

ArrayList<Sales> salesLst;

salesLst=new ArrayList<Sales>();

ArrayList<String> productName;

productName=new ArrayList<String>();

ArrayList<String> username;

username=new ArrayList<String>();

they are all submited to jsp via

session.setAttribute("salesLst", salesLst);

session.setAttribute("productLst", productName);

session.setAttribute("userLst", username);

so how do i create ArrayList of Sales, productName and username in jsp? tnx in advance

or should i got with Vector class?

PS some example code will be more than welcome

tnx

[941 byte] By [auZa] at [2007-11-26 17:33:52]
# 1

If you use JSP scriptlets inside a JSP all you have to do is enclose Java code inside

<%

%>

so in your case it would be:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<%

ArrayList<Sales> salesLst;

salesLst=new ArrayList<Sales>();

ArrayList<String> productName;

productName=new ArrayList<String>();

ArrayList<String> username;

username=new ArrayList<String>();

salesLst = session.getAttribute("salsLst");

// similarly the other two ArrayLists

%>

As you become more familiar with JSPs , you can start learning JSP Tag Libraries (JSTL) - since its the new and improved way of writing JSPs.

Although I don't really know how to use ArrayLists with JSTL in JSPs, but I guess there's probably a way to use them with JSTL.

Many articles suggest not to use Vectors since they are synchronized - using ArrayList is much better than using Vectors.

Message was edited by:

appy77

appy77a at 2007-7-9 0:01:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Just a couple of pitfalls with using this code in JSP on Tomcat.

The current compiler distributed with Tomcat5.5 is the JDT compiler.

I believe that is still only JDK1.4 compliant

ie - you can't use Java1.5 syntax in a jsp page, unless you change the internal compiler used by Tomcat

You set this in the [TOMCAT]/conf/web.xml configuration file .

In any case, scriptlet code in a JSP is discouraged.

Put that code in a Servlet/Bean/Struts action.

About the only thing you can do with a list in JSTL is read it.

You can not set values into a list using JSTL.

Of course if you are wanting to set values into a list, the code shouldn't be in a JSP anyway.

Cheers,

evnafets

evnafetsa at 2007-7-9 0:01:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

tnx for the help but tomcat has thrown exception for jsp file

<%@page import="java.util.ArrayList"%>

<%!

String date;

ArrayList<data.Sales> salesLst; //sales class is in the data package

salesLst=new ArrayList<data.Sales>();

%>

<%

date = (String)session.getAttribute("date");

salesLst=(ArrayList)session.getAttribute("salesLst");

%>

<html>

<head>

<title>Orishlajm cosmetics :: sales on <%=date%></title>

</head>

<body>

<a href="onDateSales.jsp">select another date</a>

<a href="loginAdmin.jsp">choose another option</a>

<a href="index.jsp">back to login</a>

<a href="javascript:window.print()">print this page</a>

</body>

</html>

and the exception is

An error occurred at line: 5 in the jsp file: /onDateSold.jsp

Generated servlet error:

Syntax error on token ";", , expected

but when i comment part with ArrayLIst it works

Message was edited by:

auZ

auZa at 2007-7-9 0:01:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Try

<%!

String date;

ArrayList<data.Sales> salesLst = new ArrayList<data.Sales>();

%>

or

<%!

String date;

ArrayList<data.Sales> salesLst;

%>

or

<%

String date;

ArrayList<data.Sales> salesLst = new ArrayList<data.Sales>();

%>

You're actually executing a statement where it should be a declaration.

beradriana at 2007-7-9 0:01:53 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...