• 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

Can I use object in JSP which is created in servlet?

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How should I access object, which I have created in servlet?

Servlet handles the requests(controller) and forwards to requested jsp page.
Some of the jsp pages need EJB objects. When i create an ejb object in servlet and then forward the request to jsp, how can i access the object in jsp ?

This should be MVC - based application, like JSP-Servlet-EJB.

Thanks!
[ December 07, 2005: Message edited by: Dave Thunder ]
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can attach any reference to the request - see the JavaDocs for javax.servlet.ServletRequest - specifically setAttribute and getAttribute.
Bill
 
Dave Thunder
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Problems again.

SERVLET :



JSP :


Getting NULL. Should be correct or what ?
EDIT : I just don't get it. Do I have to import anything to jsp to use request.getAttribute() ? I think no...

[ December 07, 2005: Message edited by: Dave Thunder ]
[ December 07, 2005: Message edited by: Dave Thunder ]
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first check whether you are directed to you jsp you are pointing.if you are able to direct it you should be able to get it.

regards,
sudhir.
 
Sheriff
Posts: 67746
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
This should be a straight-forward procedure. We can't tell much from the code you've posted. Are you forwarding directly to the JSP?
 
Dave Thunder
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, forwarding should be ok.

And when I set my values in session for example, I can get them in jsp.
I don't know. Maybe I will use session instead of request for setting attributes.
 
Bear Bibeault
Sheriff
Posts: 67746
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

Originally posted by Dave Thunder:
I don't know. Maybe I will use session instead of request for setting attributes.



Bad idea. Finding out why this very simple and common operation is not working for you is the best approach.

Need more info on how you are forwarding to the JSP page.
 
Dave Thunder
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whole servlet :


[ December 09, 2005: Message edited by: Dave Thunder ]
[ December 09, 2005: Message edited by: Dave Thunder ]
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're only setting the request attribute when you forward to the error.jsp page. When you forward to the tere.jsp page you aren't setting any request attributes. Was your problem with the error.jsp?
 
Dave Thunder
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, my problem is with error.jsp here. I only set attributes here for one case, it is for trying.
But it is correct, right? Should be.

And here is error.jsp :

[ December 11, 2005: Message edited by: Dave Thunder ]
 
Dave Thunder
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 some kind of mystics.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi.
I found the bug!
when you're putting your object as request attribute you use something like this:
request.setAttribute("name", new Object);

in your JSP when you trying to get attribute you use:
<%= (String)session.getAttribute("nimi")%>
rewrite that part of code to something like this:
<%= (String)request.getAttribute("nimi")%>
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't put quotes around the object that is being mapped by setAttribute. EX: request.setAttribute("KEY", OBJ);
reply
    Bookmark Topic Watch Topic
  • New Topic