954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to transfer the data from jsp to servlet ?

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

anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 

How do you redirect from the servlet to the jsp?

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 
responce .sendRedirect("user.jsp");
anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 
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");
javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

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

anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 

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

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

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

anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 
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.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

thanks javaAddict
but still its not working

anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 

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

anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

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

anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 

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: <strong>session</strong>.setAttribute("list", list);

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

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 likeAn error occurred at line: 15 in the jsp file: /ViewBalanceSheet.jsp
UserBean cannot be resolved to a type

anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 

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

javaAddict
Nearly a Senior Poster
Team Colleague
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
 

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

anand01
Posting Whiz in Training
225 posts since Aug 2010
Reputation Points: 12
Solved Threads: 20
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You