• Post Reply Bookmark Topic Watch Topic
  • New Topic

HeadFirst Servlets and JSP Page 345  RSS feed

 
Sunil Chandurkar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlet Code
foo.Person p = new foo.Person();
p.setName("Evan");
request.setAttribute("person",p);
RequestDispatcher view.....

JSP Code
<% foo.Person p = (foo.person) request.getAttribute("person"); %>
Person is <%= p.getName() %>

request.getAttribute() always comes null...
I just can't get any attributes from the servlet into the jsp

Please advise.
Thanks
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that JSP code from the book?
Are they really using scriptlets?

Also, how are you getting to the JSP?
Are you hitting it directly or are you hitting the servlet and letting it forward you to the JSP?
 
Sunil Chandurkar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,

Thanks a lot. It works when I call the Servlet which then forwards to the JSP.


If I call the JSP directly the attribute valuescome out null.

Sunil
 
Sunil Chandurkar
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben,
Yes the JSP Code is from the book and they are using scriptlets.
Sunil
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sunil Chandurkar:
It works when I call the Servlet which then forwards to the JSP. ... If I call the JSP directly the attribute valuescome out null.
Of course it will be null. If the servlet isn't called to set the scoped variable, it won't be there, and hence the null. You're not supposed to call the JSP directly without the servlet. If you do, it will fail.
 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to call the JSP directly then pass the parameters appended with the request string.

And they have used scriptlets just for illustration
 
bibi bibi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please show more your serlvet code
no just '....'
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!