Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do i invoke a bean from a servlet?

 
vijaiah
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Instantiate the bean the same way you do in a java application.
YourBean b = new YourBean();
Ajan
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jason Menard
Sheriff
Posts: 6450
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ajem Jehi
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic