• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nothing available in JSP from Servlet

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How exactly is the servlet invoking the JSP?
 
Shiwani Aggarwal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is how I invoke the jsp, is this the wrong way?
response.sendRedirect("/AddTrx1.jsp");
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Shiwani Aggarwal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do I do that? I am novice in this area.......this is my fisrt servlet/JSP....
Thanks in advance
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at ServletRequest.getRequestDispatcher() and RequestDispatcher.forward() .
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, don't use Vector. Use ArrayList.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Preeti"-
Please adjust your displayed name to meet the
JavaRanch Naming Policy.
You can change it
here.
Thanks!
Mark
 
Shiwani Aggarwal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't understand your question. Show us some code?
 
Shiwani Aggarwal
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any of you pls help me out?
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic