aspose file tools*
The moose likes Servlets and the fly likes Nothing available in JSP from Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Nothing available in JSP from Servlet" Watch "Nothing available in JSP from Servlet" New topic
Author

Nothing available in JSP from Servlet

Shiwani Aggarwal
Greenhorn

Joined: Aug 21, 2003
Posts: 22
I have created a servlet and a jsp. I have a method getname() that returns a vector of names in the servlet and this is sued as follows:
lvNames = getnames();
I used the following line to set this value so I can get this diplayed in JSP:
request.setAttribute("Names",lvNames);
In the JSP: I declare lvNames and then use the following:It gives NullPointerException. What should I do? Even if I try checking for null and then use the following, nothing is retuned in the vector, although servlets loga all valuses correctly.
lvNames = (Vector)request.getAttribute("Names");

Pls advise
Thanks
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
How exactly is the servlet invoking the JSP?


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Shiwani Aggarwal
Greenhorn

Joined: Aug 21, 2003
Posts: 22
This is how I invoke the jsp, is this the wrong way?
response.sendRedirect("/AddTrx1.jsp");
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61611
    
  67

Well, there's your answer.
In order for the attribute to be available to the JSP, you must forward from the servlet to the JSP. A redirect, such as you are doing, causes the browser to initiate a new request and so the request upon which you set the attribute is not the one that invokes the JSP.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Shiwani Aggarwal
Greenhorn

Joined: Aug 21, 2003
Posts: 22
How do I do that? I am novice in this area.......this is my fisrt servlet/JSP....
Thanks in advance
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Take a look at ServletRequest.getRequestDispatcher() and RequestDispatcher.forward() .
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Also, don't use Vector. Use ArrayList.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

"Preeti"-
Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
here.
Thanks!
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Shiwani Aggarwal
Greenhorn

Joined: Aug 21, 2003
Posts: 22
using dispatcher solved the problem, but now I have another one::
A flag if set to one value dispatches JSP - 1
setting to naother values dispatches -JSP 2
but now since it is already set, it always dispatched jsp2, how to reset that?
Thanks
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I don't understand your question. Show us some code?
Shiwani Aggarwal
Greenhorn

Joined: Aug 21, 2003
Posts: 22
The code in the servlet is:
if (request.getParameter("flag") != null)
lsFlag = request.getParameter("flag");
if (lsFlag.equals("Family"))
{
request.setAttribute("flag",lsFlag);
lPageDispatcher.showPage("/HomePage.jsp");
}
//Photo gallery and go from there
if (lsFlag.equals("Gallery"))
{
request.setAttribute("flag",lsFlag);
lPageDispatcher.showPage("/Gallery.jsp");
}

In the HomePage jsp: the flag is either set to Family or Gallery depending on which link is clicked. Now once I vidited Gallery jsp, everytime I run teh servlet, it goes to Gallery and does not bring up Home page from where I need to navigate.........I hope this clears. let me know if I confused you.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Becky",
Your display name is not valid. You can (and should) read the naming policy here
Your display name must be two words: your first name, a space, then your last name. You have been warned before.
20 posts is not good. I recommend changing your display as soon as possible to comply, otherwise it is likely your account will be deleted without warning.
thanks,
Dave
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

Well Becky, all you need to do now is add your last name to meet the Javaranch naming policy. You can use the links that I posted earlier to change it.
Thanks for your help.
Mark
Shiwani Aggarwal
Greenhorn

Joined: Aug 21, 2003
Posts: 22
Can any of you pls help me out?
Allan Wang
Greenhorn

Joined: Apr 23, 2001
Posts: 20
Hi,
The question for you is how servlet share data with JSP. Is it right?
You can do this via these steps:
In servlet:
1. save your data into request, (session, context, depending on your requirements).
2. request.getRequestDispatcher("/yourJsp.jsp").forward(request, response);
In JSP:
1. retrieve the data: Vector lvName = (Vector)request.getAttribute("Names");
"An important difference between RequestDispatcher.forward()
and HttpServletResponse.sendRedirect()is that RequestDispatcher.forward() is completely handled
on the server side while HttpServletResponse.sendRedirect()
sends a redirect message to the browser. In that sense, HttpServletRequest.
forward() is transparent to the browser while HttpServlet-
Response.sendRedirect() is not." quoted from SCWCD study kit.
What you want to do should happen all in the server side, so you shouldn't use response.sendRedirect().
Hope it helps,
allan wang
[ September 08, 2003: Message edited by: Allan Wang ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Nothing available in JSP from Servlet