File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "jsp" Watch "jsp" New topic


uday k bhan

Joined: Jul 11, 2011
Posts: 5
I am trying to make a drop down list of books from the database which the user selects and then that book as well as the quantity gets passed to the next jsp page or the cart.jsp.
this is my jsp code which is connected to a servlet which makes an array list but I am not able to pass that array list to this jsp page.
I am posting the JSp as well as the servlet code.
can some one please help?

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<%@page import="com.drop"%>
<%@page import="java.util.*" %>
<%@ page import="java.util.ArrayList" %>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Buy Books </title>
<h1> Available Books</h1></center>
String name=(String)session.getAttribute("user");
Logged in as: <%=name %>


Select the book from the list of available books in our warehouse:

<CENTER> Select Book :

ArrayList array1=(ArrayList)request.getAttribute("result");

Iterator iterator = array1.iterator();

out.println("<select name=selected_book>");

String temp =;

out.println("<option value=\"" + temp + "\">" + temp + "</option>");



<% String current=(String)session.getAttribute("user");

<%= current%>

<form action=item method="post">
Quantity : <select name="qty" >
<option value="0">--</option>
<option value="1">1</option>
<option value="2">2</option></select>

<input type="Submit" value='ADD TO CART'>


package com;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.catalina.connector.Request;

import oracle.jdbc.pool.OracleDataSource;
import java.sql.*;
import java.util.*;
import java.util.*;
public class drop extends HttpServlet
private static final long serialVersionUID = 1L;

public drop(){

ArrayList<String> b1=new ArrayList<String>();

public ArrayList<String> book()
try {

OracleDataSource ds = new OracleDataSource();
ds.setDatabaseName("XE"); // sid

Connection conn = ds.getConnection();

Statement stmt =null;
ResultSet rs=stmt.executeQuery("select * from books");
String s;
while (


catch(Exception ex)
System.err.println("Exception: " + ex.getMessage());
return b1;

public void doPost(HttpServletRequest request, HttpServletResponse response)
RequestDispatcher rd = getServletContext().getRequestDispatcher("/buybook.jsp");
try {
rd.forward(request, response);
} catch (ServletException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 547


As Bear mentioned, please use code tags to improve the readability. Coming to the problem, try printing the arraylist in your get/post methods before stuffing to session. Secondly, you are using arraylist as an instance variable of the servlet, and remember container would use a new thread for handling individual requests based on the container configuration, you can not rely on instance variables. So, instead return the list from book() method.

I'm not sure, where did you set the user attribute to session, but I would assume that you would have already made an arrangement of it. I see that you tried to print the results in jsp. What is happening there? Are you seeing anything being displayed. I also see that your books in select tag are not in enclosed in form tag. So, if you submit the form, you are not sending the selected book.

Just as an additional advice, avoid using scriptlets and start using Expression language and custom tags, as usage of scriptlets in jsp is outdated and not recommended.


uday k bhan

Joined: Jul 11, 2011
Posts: 5
Hi Raja,
thanks a lot for your help.
when I run the existing code i get NullPointer Exception

1) I am currently returning my array list b1 in the book() which is called in the post method.
Are you saying that I should do all the connection to database and also the arraylist in the post method directly instead of making a book() method?

2) I have a session created in the login page where the session attribute is set to the sername of the person who logs in.
even when I place the select tag nothing is passed .
Can you please guide me as I am new to JSP and also how to using EL code instead of the existing java code in my jsp as i tried but failed to get it running.If you can write a EL code snippet I will really appreciate it.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

probably you are geting array1 as null value. the reason is you were setting the object[key="result"] in session in your servlet and in jsp you are searching in request? got the point?

for your EL problem, did you check this?
first of all, if you get any problem regarding jsp, dont forget to check JSP faq before asking question. you can find the link for JSP FAQ in *Bear signature*
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63858

As pointed out, scriptlets are a very very poor practice. If you are having trouble getting the JSTL and EL working, that is where you need to be expending your effort rather than tracking down silly null-pointer exceptions in scriptlet code. Please try to get the EL and JSTL working and then post with any difficulties you are having. Trying to trace the problems you have reported in this post is a waste of your time.

Also, if you are new to JSP, recommend you read the following articles:
  • The Secret Life of JSPs
  • The Front Man

  • [Asking smart questions] [About Bear] [Books by Bear]
    uday k bhan

    Joined: Jul 11, 2011
    Posts: 5
    Thanks Seetharaman I was able to get the flow right and I could pass the arraylist generated in the servlet to the JSP page and I could display that as a dynamic drop down too but now when I select the option in the drop down that I made with the help of an arraylist passes null value to the next servlet on submit.
    I have a quantity field its value I am able to get by doing request.getParamater but not the drop down value I am able to get by doing the same.
    I am posting the code of my JSP and A ver simple servlet
    Please help.

    uday k bhan

    Joined: Jul 11, 2011
    Posts: 5
    can some one please help me out and point out my mistake...I am really stuck
    Paul Clapham

    Joined: Oct 14, 2005
    Posts: 19973

    So here's the HTML form which is being used:

    And as far as I can see you are asking why there is no "nval" parameter in that form. Is that your question? Because there isn't one. You shouldn't be expecting to get an "nval" parameter when that form is submitted.
    uday k bhan

    Joined: Jul 11, 2011
    Posts: 5
    thank you so much paul
    I agree. Here's the link:
    subject: jsp
    It's not a secret anymore!