Hi all ,
I need to transfer data from jsp to servlet..
am accessing db from servlet .. I need to tranfer those db content to my jsp page how can I achieve that .
thanks in advance

How do you redirect from the servlet to the jsp?

responce .sendRedirect("user.jsp");
responce .sendRedirect("user.jsp");

For single values that approach could work. You can use this:

String value1 = "aaaa";
String value2 = "bbbbb";

responce.sendRedirect("user.jsp?param1="+value1+"&param2="+value2);

At the jsp you can use this:

String param1 = request.getParameter("param1");
String param2 = request.getParameter("param2");

When sending data like that use the .getParameter. But that won't work if you have one instance of an object and you want to send the whole thing

If you want to send an instance of an object use this at the servlet instead of the sendRedirect

SomeObject obj = get_from_somewhere;
request.setAttribute("SOME_KEY", obj);

RequestDispatcher dispatcher = request.getRequestDispatcher("user.jsp");
dispatcher.forward(request, response);

At the jsp:

SomeObject obj = (SomeObject)request.getAttribute("SOME_KEY");

i need to transfer array list ...
my code is

DataManager

public ArrayList<UserBean> getBalanceSheet(int int_month,int int_year) {
			// TODO Auto-generated method stub
	    	ArrayList<UserBean> list = new ArrayList<UserBean>();
	    	
			try{
	    		query ="SELECT " +
	    				"`invoiceid` , `date` , `invoicetype` , `invoice_from_to` , `invoicedescription` , `rep` , `amount` " +
	    				"FROM " +
	    				"`invoicetable` " +
	    				"WHERE " +
	    				"MONTH( date ) = ? " +
	    				"AND YEAR( date ) = ?";
pstm = con.prepareStatement(query);
pstm.setInt(1,int_month);
pstm.setInt(2,int_year);	
rs=pstm.executeQuery();
while(rs.next())
				{
UserBean user = new UserBean();
					             user.setInvoiceIid(Integer.parseInt(rs.getString("invoiceid")));
user.setDate(rs.getString("date"));
user.setInvoiceType(rs.getString("invoicetype"));
user.setInvoiceFromTo(rs.getString("invoice_from_to"));
					user.setInvoiceescription(rs.getString("invoicedescription"));
					user.setRep(rs.getString("rep"));
					user.setAmount(new Float(rs.getString("amount")));
					list.add(user);
				}		
		}

my servlet code

dataManager.getConnection();
		list =new ArrayList<UserBean>(dataManager.getBalanceSheet(int_month,int_year));

I need to transfer those data from servlet to jsp page
pls help me

list is an objct. So put into the request (request.setAttribute) like my second example

hi javaAddict,
Thanks
But here i need to transfer array list to jsp how can I do that..

hi javaAddict,
Thanks
But here i need to transfer array list to jsp how can I do that..

So put into the request (request.setAttribute) like my second example

I already told you. ArrayList is an object like any other. Use the second example with the get/set Attribute and put in the the request the ArrayList instance.

thanks javaAddict
but still its not working

Hi all,
This is duplicate thread of

http://www.daniweb.com/web-development/jsp/threads/380026

actually I need to transfer the list of data from servlet to jsp. i tried lot ut its not working
my servlet code is

dataManager.getConnection();
		list =new ArrayList<UserBean>(dataManager.getBalanceSheet(int_month,int_year));
		session.setAttribute("list", list);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/ViewBalanceSheet.jsp");
		dispatcher.forward( request, response);

i can access these data in servlet itself but i am not able to transfer to my jsp page

my jsp code is

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList"%>
<jsp:useBean id="userBean" class="UserBean" scope="session"></jsp:useBean>
<jsp:useBean id="list"type="ArrayList<UserBean>" scope="session" ></jsp:useBean>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%

UserBean user = new UserBean();
for(int i=0; i < list.size(); i++)
{
	user = (UserBean) list.get(i);
	out.println(user.getRep());
}
%>
</body> 
</html>

But it shows error
pls help me

Thanks in advance

Threads merged. Please don't start additional threads for a single question.


actually I need to transfer the list of data from servlet to jsp. i tried lot ut its not working
my servlet code is

dataManager.getConnection();
		list =new ArrayList<UserBean>(dataManager.getBalanceSheet(int_month,int_year));
		session.setAttribute("list", list);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/ViewBalanceSheet.jsp");
		dispatcher.forward( request, response);

i can access these data in servlet itself but i am not able to transfer to my jsp page

my jsp code is

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.ArrayList"%>
<jsp:useBean id="userBean" class="UserBean" scope="session"></jsp:useBean>
<jsp:useBean id="list"type="ArrayList<UserBean>" scope="session" ></jsp:useBean>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%

UserBean user = new UserBean();
for(int i=0; i < list.size(); i++)
{
	user = (UserBean) list.get(i);
	out.println(user.getRep());
}
%>
</body> 
</html>

But it shows error
pls help me

Thanks in advance

You are completely ignoring my suggestions and especially the second example in my first post. Thank you for making waste my time to give you the solution when you simply ignored me.

Dear javaAddict,
Am not ignoring you . You already solved many of my previous threads. i must thankful for you.
Am not the person who ignores seniors suggestion .

Actually i used your second example

RequestDispatcher dispatcher = request.getRequestDispatcher("user.jsp");
dispatcher.forward(request, response);

but it shows null pointer exception.i was frustrated i am keep on trying pls help me

Dear javaAddict,
Am not ignoring you . You already solved many of my previous threads. i must thankful for you.
Am not the person who ignores seniors suggestion .

Actually i used your second example

RequestDispatcher dispatcher = request.getRequestDispatcher("user.jsp");
dispatcher.forward(request, response);

but it shows null pointer exception.i was frustrated i am keep on trying pls help me

My example said:

request.setAttribute("...",obj);

RequestDispatcher dispatcher = request.getRequestDispatcher("user.jsp");
dispatcher.forward(request, response);

And: ArrayList obj = (ArrayList)request.getAttribute("list"); You wrote at your post: [B]session[/B].setAttribute("list", list);

ya i tried like that also
my servlet code

list =new ArrayList<UserBean>(dataManager.getBalanceSheet(int_month,int_year));
		request.setAttribute("listvalue", list);
		RequestDispatcher dispatcher = request.getRequestDispatcher("/ViewBalanceSheet.jsp");
	dispatcher.forward( request, response);

my jsp code

<%

UserBean user = new UserBean();
ArrayList<UserBean> list;
list =(ArrayList<UserBean>)request.getAttribute("listvalue");
for(int i=0;i<=list.size();i++)
	{
	user = (UserBean) list.get(i);
	out.println(user.getAmount());
	}

%>

but =it shows error like

An error occurred at line: 15 in the jsp file: /ViewBalanceSheet.jsp
UserBean cannot be resolved to a type

If this is the error that you get:
UserBean cannot be resolved to a type
then:
When you use the UserBean in the servlet, does it compile?
If it compiles and the jsp doesn't compile then I think it is because you haven't import it.

If you still can't fix it, post the whole code of the jsp, pointing where is the line that you get the error is

Dear javaAddict,
Thanks a lot ,
Finally its working
I made mistakes in import statements ..
Thanks a lot once again..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.