Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Dave Thunder
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • 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 ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • 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
  • 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 ]
 
sudhir kamath
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • 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.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
Author and ninkuma
Marshal
Posts: 65824
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Whole servlet :


[ December 09, 2005: Message edited by: Dave Thunder ]
[ December 09, 2005: Message edited by: Dave Thunder ]
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
This is some kind of mystics.
 
Taras Tovstenko
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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")%>
 
Jeremy McNally
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't put quotes around the object that is being mapped by setAttribute. EX: request.setAttribute("KEY", OBJ);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!