0

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

3
Contributors
16
Replies
20
Views
5 Years
Discussion Span
Last Post by anand01
Featured Replies
  • [QUOTE=anand01;1636902][code] responce .sendRedirect("user.jsp"); [/code][/QUOTE] For single values that approach could work. You can use this: [CODE] String value1 = "aaaa"; String value2 = "bbbbb"; responce.sendRedirect("user.jsp?param1="+value1+"&param2="+value2); [/CODE] At the jsp you can use this: [CODE] String param1 = request.getParameter("param1"); String param2 = request.getParameter("param2"); [/CODE] When sending data like that use the … Read More

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

Edited by anand01: n/a

1
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");
0

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

Edited by anand01: n/a

0

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

Edited by javaAddict: n/a

0

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

0

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.

0

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

0


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.

0

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

Edited by anand01: n/a

0

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);

0

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

0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.