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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Passing object from Servlet to JSP

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Dear Everyone,

I am getting NullPointerException in the JSP in the line <% for (int i=0;i<a.size();i++)
Please correct the error. I am a beginner that's why I am using scriptlet.

Regards,
Salman

package net.codejava;

public class Category {
private int id;
private String name;

public Category(int id, String name) {
super();
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}




--------------------------------------------------------------------

package net.codejava;



import java.io.Serializable;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class DAO
{
Connection con ;
{
try
{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstoredb","root","");
}
catch(java.lang.Exception ex)
{
}
}

public Category list()
{
String sql = " SELECT category_id,name FROM category where name = 'JSP' ";
Statement statement;
try {
statement = con.createStatement();
ResultSet rs = statement.executeQuery(sql);

if(rs.next())
{
int id = rs.getInt("category_id");
String name = rs.getString("name");
Category x = new Category(id, name);
rs.close();
return x;
}
else
{
return null;
}
}
catch (SQLException e)
{
return null;
}

}

}


--------------------------------------------------------------------


package net.codejava;



import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ListServlet extends HttpServlet {

protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException
{

DAO e= new DAO();

Category t = e.list();

int a= t.getId();
String b = t.getName();

Category product= new Category(a,b);

request.setAttribute("product",product);

String url = "/index.jsp";

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);

}



@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{


}
}


--------------------------------------------------------------------


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@page import="net.codejava.Category"%>
<%@page import="net.codejava.DAO"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.ArrayList"%>
<html>

<head>
<title></title>
</head>

<%
ArrayList<Category> a = (ArrayList<Category>)request.getAttribute("listCategory");
%>
<body>

<table cellspacing="5" border="0">

<tr>
<td align="right"><p>Category:</td>
<td><select name="category">
<% for (int i=0;i<a.size();i++)
{
Category g = a.get(i);
%>
<option value="<%=g.getId() %>" >
<%=g.getName() %>
</option>

<%
}
%>
</select>
</td>
</tr>

</table>
</body>
</html>

-------------------------------------------------------------

MYSQL

create database bookstoredb;

use bookstoredb;

CREATE TABLE `category` (
 `category_id` int(11) NOT NULL,
 `name` varchar(30) NOT NULL
);

INSERT INTO `category` (`category_id`, `name`) VALUES
(1, 'Java'),
(2, 'PHP'),
(3, 'C'),
(4, 'JSP');

 
Saloon Keeper
Posts: 28753
211
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
This is just a duplicate of your earlier thread at https://coderanch.com/t/754832/java/Passing-ArrayList-Servlet-JSP

Reposting or "bumping" a thread will not gain you anything on the Ranch. If someone wants to answer, they will answer.

So I'm locking this duplicate to avoid confusing people.

Using scriptlets just because "you are a beginner" is no excuse, though. Scriptlets are basically obsolete, and there is a Bear on this forum who will growl at you if you use them.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic