File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

jsp

uday k bhan
Greenhorn

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 drop.java 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?

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

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="com.drop"%>
<%@page import="java.util.*" %>
<%@ page import="java.util.ArrayList" %>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Buy Books </title>
<center>
<h1> Available Books</h1></center>
</head>
<body>
<%
String name=(String)session.getAttribute("user");
if(name==null)
{
response.sendRedirect("Error.jsp");
}
%>
Logged in as: <%=name %>

<center>



</center>
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>");

while(iterator.hasNext())
{
String temp = iterator.next().toString();

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

out.println("</select>");

%>



<% 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'>
</form>
</center>
</body>
</html>



SERVLET:


package com;
import java.io.IOException;

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.io.*;
import java.sql.*;
import java.util.*;
import java.util.*;
public class drop extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;


public drop(){
super();
}

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


public ArrayList<String> book()
{
try {

OracleDataSource ds = new OracleDataSource();
ds.setDriverType("thin");
ds.setServerName("127.0.0.1");
ds.setPortNumber(1521);
ds.setDatabaseName("XE"); // sid
ds.setUser("system");
ds.setPassword("Jai");

Connection conn = ds.getConnection();

Statement stmt =null;
stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("select * from books");
String s;
while (rs.next())
{
s=rs.getString(2);
b1.add(s);

}

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

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

Joined: Mar 18, 2010
Posts: 522
    
    2

Uday,

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.


Regards
KumarRaja

uday k bhan
Greenhorn

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
Marshal

Joined: Jan 10, 2002
Posts: 61610
    
  67

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] [Bear's FrontMan] [About Bear] [Books by Bear]
    uday k bhan
    Greenhorn

    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 item.java.
    Please help.

    buybook.jsp


    item.java
    uday k bhan
    Greenhorn

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

    Joined: Oct 14, 2005
    Posts: 18886
        
        8

    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
    Greenhorn

    Joined: Jul 11, 2011
    Posts: 5
    thank you so much paul
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: jsp