How do you redirect from the servlet to the jsp?
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
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+"¶m2="+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
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
list is an objct. So put into the request (request.setAttribute) like my second example
javaAddict
Nearly a Senior Poster
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..
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
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
Threads merged. Please don't start additional threads for a single question.
Ezzaral
Posting Genius
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
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
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
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
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
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448