problem with request.getAttribute()

problem with request.getAttribute()

Page 1 of 1

6 Replies - 23018 Views - Last Post: 05 March 2009 - 06:41 AM Rate Topic: -----

#1 priya_1222  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 28-February 09

problem with request.getAttribute()

Post icon  Posted 28 February 2009 - 07:51 AM

This is my servlet code. I m using setAttribute to set value of
Iname. Then i forward the request to jsp page. On my jsp page i m trying to print the value of Iname by using getAttribute but i get error saying [Ljava.lang.String;@14c5b37

 

				int i=0;
				while(rs.next())
	{
						name  =rs.getString("item_name");
						number=rs.getInt("item_no");
						price =rs.getInt("price");
				
						Iname[i] = name; 
						Inumber[i]=number;
						Iprice[i]=price;
						i++;
	}
					
					req.setAttribute("itemName",Iname); 		
				RequestDispatcher dis = req.getRequestDispatcher("/Test/DisplayPage.jsp");
	if(dis != null)
	dis.forward(req,res);



this is my jsp page

<%@ page language="java"%>

<html>
<head>
<title>Entry Form</title>

</head>

<body>

	<%=request.getAttribute("itemName")%>
	
	
</body>
</html>




some one please help me to understand this problem.
Thank you.

This post has been edited by priya_1222: 28 February 2009 - 07:54 AM


Is This A Good Question/Topic? 0
  • +

Replies To: problem with request.getAttribute()

#2 priya_1222  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 28-February 09

Re: problem with request.getAttribute()

Posted 03 March 2009 - 06:18 AM

Hello every one i m new on this site. I had added one query to this forum
but no one is replying.
I request some one to help me out Please.

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: problem with request.getAttribute()

Posted 03 March 2009 - 04:41 PM

Not sure what "req" is but you set it to Iname which is an array of String not a single String
Does your JSP expects an array ?
Was This Post Helpful? 0
  • +
  • -

#4 priya_1222  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 28-February 09

Re: problem with request.getAttribute()

Posted 04 March 2009 - 02:43 AM

Thank you so much for replying

Sir "req" is my HttpServletRequest object.

I m using servlet to retrive all values from my database table.
I m storing this values in array.
Now i want to display all this values on jsp page.
so i need the entire array on jsp page.

Was This Post Helpful? 0
  • +
  • -

#5 ucan_aps  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 31
  • Joined: 26-December 08

Re: problem with request.getAttribute()

Posted 04 March 2009 - 06:30 AM

View Postpriya_1222, on 4 Mar, 2009 - 01:43 AM, said:

Thank you so much for replying

Sir "req" is my HttpServletRequest object.

I m using servlet to retrive all values from my database table.
I m storing this values in array.
Now i want to display all this values on jsp page.
so i need the entire array on jsp page.

hi priya..
store the itemName in array when getting by getAttribute in jsp..
and iterate array values..

i havent checked this one.. try this.. it may help..
Was This Post Helpful? 1
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: problem with request.getAttribute()

Posted 04 March 2009 - 06:31 PM

This

[Ljava.lang.String;@14c5b37

really looks that doing a System.out.println() on an array rather than on an array element
Was This Post Helpful? 0
  • +
  • -

#7 priya_1222  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 5
  • Joined: 28-February 09

Re: problem with request.getAttribute()

Posted 05 March 2009 - 06:41 AM

Thank you every one for helping me.
I have solved my problem. I have used ArrayList.

import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;


public class ExtractEntries extends HttpServlet
{
	
Connection con;
String url="jdbc:odbc:pract";

public void init(ServletConfig config) throws ServletException
{
		 try
	{	
	Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
	con= DriverManager.getConnection(url);
	}
	catch(Exception e)
	{
	System.out.println("Error in init:" +e);
	}
}	

public void doPost(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException
{	
	Statement stmt;
	ResultSet rs;	
		
	ArrayList Iname  = new ArrayList();
	ArrayList Inumber= new ArrayList();
	ArrayList Iprice = new ArrayList();

	String name= null;
	int number=0, price=0;	
	
				res.setContentType("text/html");
	PrintWriter out = res.getWriter();
			
	if(con == null)
	out.println("Not connected to the Database");
	else
	{
	out.println("You are connected to the Database");
	try
	{
	stmt = con.createStatement();
	int empid = Integer.parseInt(req.getParameter("eid"));
	String str = "select * from item_details where					   
				eid="+empid+"";

	rs = stmt.executeQuery(str);
		
	while(rs.next())
	{
		name  =rs.getString("item_name");
		number=rs.getInt("item_no");
		price =rs.getInt("price");
								Iname.add(name);
		Inumber.add(new Integer(number));	
		Iprice.add(new Integer(price));									}

				req.setAttribute("itemName",Iname); 
	req.setAttribute("itemNumber",Inumber); 		
	req.setAttribute("itemPrice",Iprice); 				
	RequestDispatcher dis = req.getRequestDispatcher			   
				 ("/Test/DisplayPage.jsp");

	if(dis != null)
	dis.forward(req,res);

				 }// end of try
	
	catch(Exception e)
	{
	out.println("Error:" +e);
	}

				}			

}

}


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

And my jsp page is as

<%@ page language="java" import="java.util.*" %>

<html>
<head>
<title>Entry Form</title>

</head>

<body>
<br><br>
<center>
<table border=2 width=400>
<tr align=left>
<th>Name</th>
<th>Code</th>
<th>Price</th>
<tr>

<% 
	try
	{
	if(request.getAttribute("itemName")!=null)
					 {
																			
		ArrayList name =(ArrayList)request.getAttribute("itemName");
		ArrayList code =(ArrayList)request.getAttribute("itemNumber");
		ArrayList price=(ArrayList)request.getAttribute("itemPrice");

		
		for(int i=0;i<name.size();i++)
		{
%> 			
	<tr>
	<td> <%=name.get(i)%> &nbsp;</td>
		   	<td> <%=code.get(i)%> &nbsp;</td>
	<td> <%=price.get(i)%>&nbsp;</td>
	</tr>
<%
		}
			  
	  }
  else
	  out.println("No data in array");
	}
	
	catch(Exception e)
	{
		out.println("Error" +e);
	}
		
%>	
	
</table>
</center>
</body>
</html>





Was This Post Helpful? 0
  • +
  • -

Page 1 of 1