aspose file tools
The moose likes JSP and the fly likes Can I use object in JSP which is created in servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "Can I use object in JSP which is created in servlet?" Watch "Can I use object in JSP which is created in servlet?" New topic
Author

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

Dave Thunder
Greenhorn

Joined: Dec 03, 2005
Posts: 22
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

Joined: Mar 22, 2000
Posts: 12271
    
    1
You can attach any reference to the request - see the JavaDocs for javax.servlet.ServletRequest - specifically setAttribute and getAttribute.
Bill


Java Resources at www.wbrogden.com
Dave Thunder
Greenhorn

Joined: Dec 03, 2005
Posts: 22
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

Joined: Mar 07, 2002
Posts: 16
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

Joined: Jan 10, 2002
Posts: 56207
    
  13

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?


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Dave Thunder
Greenhorn

Joined: Dec 03, 2005
Posts: 22
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

Joined: Jan 10, 2002
Posts: 56207
    
  13

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

Joined: Dec 03, 2005
Posts: 22
Whole servlet :


[ December 09, 2005: Message edited by: Dave Thunder ]
[ December 09, 2005: Message edited by: Dave Thunder ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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

Joined: Dec 03, 2005
Posts: 22
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

Joined: Dec 03, 2005
Posts: 22
This is some kind of mystics.
Taras Tovstenko
Greenhorn

Joined: Dec 20, 2005
Posts: 1
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")%>
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Can I use object in JSP which is created in servlet?
 
Similar Threads
How do i access locales created in EJB from JSP.
How do i access locales created in EJB from JSP.
EJB Access
EJB access from a JSP in VAJ Test Environment
stateless session bean's ejbCreate method