0
votes

I have created a web application that uses a login page, a servlet, a managed bean, and a jsp page. No matter what I have tried I cannot get the JSP page to display an array of item names from my servlet that are acquired using an arraylist of bean objects. I even tried just creating a single bean object in the servlet and then passing just a string variable. I have tried using both session and request and cannot get this to work here is my code:

Servlet:

package xxxx;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class ItemServlet extends HttpServlet {


protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
 ItemInventory iI = new ItemInventory();
 String[] itemNames = iI.getNames();
 HttpSession session = request.getSession();
 session.setAttribute("itemNames", itemNames);
 request.setAttribute("itemNames", itemNames);
 RequestDispatcher rd = request.getRequestDispatcher("/secureApps/ItemsCatalog.jsp");


 rd.forward(request, response);



}



@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}


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

}


@Override
public String getServletInfo() {
    return "Short description";
}
}

JSP:

<%@page import="xxxx.ItemInventory"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%

String[] itemName = (String[])session.getAttribute("itemNames");

%>


<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Item Catalog</title>
</head>
<body>
    <h1>Item Catalog</h1>
    <Select Name="itemNames">
        <% for(int i=0;i < itemName.length; i++){
         %>
         <option value ="<%= itemName[i]%>"><%= itemName[i]%></option>

         <%
         }
        %>

    </select> 
         ${itemBean.itemID}
</body>
</html>

The Bean

package xxxx;


import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


@ManagedBean
@SessionScoped
public class ItemBean {

String itemName;
String itemID;
String itemDescription;
String itemPrice;
String itemQuantity;


public ItemBean(){}

public ItemBean(String iD, String name, String description, String price, String quantity){

    this.itemID = iD;
    this.itemName = name;
    this.itemDescription = description;
    this.itemPrice = price;
    this.itemQuantity = quantity;


}
public String getItemName(){
    return itemName;
}


}

ItemInventory Class:

package xxxx;

import java.io.BufferedReader;

import java.io.FileReader;
import java.io.IOException;

import java.util.ArrayList;


public class ItemInventory {

ArrayList <ItemBean> items = new ArrayList<ItemBean>();

public static void main(String[] args){}


public void ItemInventory() throws IOException{

  BufferedReader br = new BufferedReader(new FileReader("/catalog.txt")); 
  String text = null;





      while ((text=br.readLine())!=null){
          String[] itemArray = text.split(",");
          items.add(new ItemBean (itemArray[0], itemArray[1], itemArray[2], itemArray[3], itemArray[4]));





  }

   br.close();

} 

public Integer getSize(){
    Integer x = items.size();
    return x;
}

public String[] getNames(){
    int y = items.size();
    String[] itemNames = new String[y];
    for (int i = items.size() - 1; i >=0; i--){

        itemNames[i] = items.get(i).getItemName();
    }
    return itemNames;
}

}

Please help, I have been at this for hours....

2
Did you checked size of itemNames.length in Servlet ?Hardik Mishra
Just did and it is coming backs as 0.....now I am even more confusedssgtob1
Yeah. ItemInventory iI = new ItemInventory(); What is happening there ?Hardik Mishra
Sorry, forgot to add that class....it is there nowssgtob1

2 Answers

0
votes

There is nothing wrong with your Servlet / JSP code.

Here, int y = items.size(); This will always return 0 as you have initialized it earlier.

You are misunderstanding the some concepts here. When you create an object of the Class main() will not be executed directly. So, Here in your case it returns empty Array.

You should remove the code from main() and may be write a separate method to do that. Also, Its bad practice to have business logic inside constructor

Example: ItemInventory.java

public void fillItems(){
    try{
        BufferedReader br = new BufferedReader(new FileReader("/catalog.txt")); 
        String text = null;
        while ((text=br.readLine())!=null){
           String[] itemArray = text.split(","); 
           // you might want to check array size
           items.add(new ItemBean (itemArray[0], itemArray[1], itemArray[2], itemArray[3], itemArray[4]));

        }

        br.close();
    }catch(Exception e){
      e.printStackTrace();
    }
}

In Servlet

ItemInventory iI = new ItemInventory();
iI.fillItems();
String[] itemNames = iI.getNames();
0
votes

You did not provide code of ItemInventory so hard to confirm if iI.getNames() really return an array having data.

The code looks correct to me so it should work if iI.getNames() return data.

Check the length of the array.

Also I see that you are expecting a itemBean but not provided in the session or request scope.

Also the itemBean is not used on the servlet either.

Am I missing something?