aspose file tools*
The moose likes Servlets and the fly likes How do i invoke a bean from a servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "How do i invoke a bean from a servlet?" Watch "How do i invoke a bean from a servlet?" New topic
Author

How do i invoke a bean from a servlet?

vijaiah
Greenhorn

Joined: Feb 08, 2001
Posts: 3
i want to invoke a bean from a servlet from which it is connecting to the data base.
So can i invoke a bean from a servlet??
Thanks in advance..
-Vijay.v.
Ajan Balakrishnan
Ranch Hand

Joined: Jan 04, 2001
Posts: 71

Instantiate the bean the same way you do in a java application.
YourBean b = new YourBean();
Ajan
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
Or if the bean has already been created and put in the session from a JSP, you should be able to just get it from the session and use it.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
We'll assume for this example that your bean is called myBean of type MyBean, and that the bean is session scope. For application you would use ServletContext not HttpSession.

If the bean is application scope, you would do pretty much the same thing except you would use the getServletContext() method that HttpServlet inherits from GenericServlet.

Request scope is like this.

Hope this helps answer your question.
[This message has been edited by Jason Menard (edited February 20, 2001).]
Ajem Jehi
Greenhorn

Joined: Sep 06, 2013
Posts: 10
Frank Carver wrote:Or if the bean has already been created and put in the session from a JSP, you should be able to just get it from the session and use it.


Frank,

This is sounding like my case. I am planning to use a servlet to populate iframe's HTML (as it is a trivial single-pass operation with no action forms).
The iframe is on a JSP page which already loaded the bean.
Could you provide a sample of how I can reference the bean from the servlet in the iframe?

Looks like Jason Menard already answered most of that, but I am just not 100% sure whether I need to instantiate the bean in the doGet method, or processRequest method.

Tried this:

In JSP:



I know it calls the servlet as it prints some diagnostic messages I put in.

In servlet's processRequest:




This does not work as I see

Context's blConfig = null
Session's blConfig = null
Request's blConfig = null

where the iframe should be.

Thanks a lot!
ajeh
Ajem Jehi
Greenhorn

Joined: Sep 06, 2013
Posts: 10
So far it sounds like I absolutely have to use at least one iframe simply because I need a panel, larger than space it can occupy, scrollable in both directions.
The iframes are separate HTTP requests and it seems that if they are requested simultaneously with the JSP page, then they end up in the different sessions.
I tried to first create a separate JSP page and instantiate a bean in it, then submit a form to my main JSP.
But the trick did not work, the servlet in the iframe is still in a separate session. Can anyone suggest a method to share a bean among JSP and its iframe'd servlets?

Without being able to access the bean from servlets I am totally stuck.

Thank you!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Ajem Jehi wrote:So far it sounds like I absolutely have to use at least one iframe simply because I need a panel, larger than space it can occupy, scrollable in both directions.

No. You do not need to use an iframe. But this topic has nothing at all to do with beans and servlets, so I suggest you start a post in the HTML forum asking how to create a scrollable area without need for an iframe.

The iframes are separate HTTP requests and it seems that if they are requested simultaneously with the JSP page, then they end up in the different sessions.

Only if you do something wrong. There is no reason for them not share the session.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ajem Jehi
Greenhorn

Joined: Sep 06, 2013
Posts: 10
Bingo! It is inside the session under the variable name initially assigned by usebean!

I was going bonkers trying to find it under the bean's class name instead. Silly noob!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i invoke a bean from a servlet?