wood burning stoves*
The moose likes Servlets and the fly likes Getting JSP bean from Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Getting JSP bean from Servlet" Watch "Getting JSP bean from Servlet" New topic
Author

Getting JSP bean from Servlet

Steven Marco
Greenhorn

Joined: Jul 23, 2002
Posts: 21
If in the JSP I have:

<jsp:useBean id="test" class="MyTest" scope="request" />

In the Servlet I have:

MyTest test = new MyTest();
MyTest myTest= (MyTest)request.getattribute("test");


However "myTest" will be empty when running, what's wrong with the above? Thanks.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
How is the request getting from the jsp to the servlet (usually the other way around or it is a different request scope)? Show us the jsp code.

But to get a request parameter use request.getParameter("test") not getAttribute().
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

This doesn't quite solve your problem, but it might help you find out what IS in your request.

This little chunk of code is priceless:




If you use this, you can loop through all the attributes, or parameters in your request, and see what's in there. It can eliminate spelling mistakes and stuff pretty quickly.

Cheers!

-Cameron McKenzie
Donna Bachner
Ranch Hand

Joined: Oct 08, 2004
Posts: 109
I have a similar problem. I have a JSP and want to call the Servlet also. I have a link I want to take to the servlet. I can go to another JSP fine but I can't get the resulets I want. From the jsp I was using a a href to the other jsp but it does not work for the servlet. Any ideas?
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

How about session scoped attributes or hidden variables ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60048
    
  65

Originally posted by Donna Bachner:
I have a similar problem. I have a JSP and want to call the Servlet also. I have a link I want to take to the servlet. I can go to another JSP fine but I can't get the resulets I want. From the jsp I was using a a href to the other jsp but it does not work for the servlet. Any ideas?


Once the response for the first JSP is sent to the browser, that request goes out of scope so any scoped variabled placed on it will not be available for a new request generated by clicking on a link in the page.

For a scoped variable to be available to multiple requests, you must use session scope (for user-specific data) or application scope (for application-shared data).

You could also use hidden variables on the page to store information that will be submitted back to the next request.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Steven Marco:
If in the JSP I have:

<jsp:useBean id="test" class="MyTest" scope="request" />

In the Servlet I have:

MyTest test = new MyTest();
MyTest myTest= (MyTest)request.getattribute("test");


However "myTest" will be empty when running, what's wrong with the above? Thanks.


Two things:

1.)
See these two links to understand why it's important that your beans be packaged:
http://faq.javaranch.com/view?PackageYourBeans
http://faq.javaranch.com/view?BeansNotFound
They're short.

2.)
Are you forwarding, redirecting, or getting from the JSP to the servlet by drawing the webpage and providing a link?
Of the above, only the first will allow you to use request scope.
For the others, you'll need to use session scope.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Donna Bachner:
I have a similar problem. ...


Donna,
It would be better for you to start a new thread with your problem.
Unless you're issue has the same cause as the original poster's, any help you get here will detract from the original question and result in a hijacked thread.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting JSP bean from Servlet
 
Similar Threads
Static Method/Non-Static Method, RTTI
Reg. abstract class and method
HttpSessionBindingListener
Shallow in static method ?
regarding servlet-mapping