wood burning stoves 2.0*
The moose likes Servlets and the fly likes Servlet equivalent to <jsp:useBean ../> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet equivalent to <jsp:useBean ../>" Watch "Servlet equivalent to <jsp:useBean ../>" New topic
Author

Servlet equivalent to <jsp:useBean ../>

C Vivek
Ranch Hand

Joined: Oct 03, 2004
Posts: 115
<jsp:useBean id="obj" scope="session" class="classname"/>
<%=obj.display()%>


Please could you tell me what Servlet equivalent is for the above code?

Regards,
Vivek
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

Create an instance of the class, set it into session scope.

<jsp:useBean> also has some semantics around checking whether the bean already exists or not.

You could also look at the generated JSP code to see how the action is implemented.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
You have to import the class using page directive
<%@page import="complete class path">

and then create an instance of the bean and add it to the session using setAttribute() method.


krad
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60748
    
  65

kranthi adari wrote:You have to import the class using page directive
<%@page import="complete class path">

That makes no sense in the context of a servlet.
kranthi adari
Greenhorn

Joined: Dec 29, 2008
Posts: 21
In a servlet, you cannot use page-directive, Sorry. Please find my steps below to implement <jsp:usebean> equivalent in servlet.

Import the bean using import <bean-path>
and then create a session object
HttpSevletSession sesssion = request.getSession();
Now create a bean object
MyBean b = new MyBean();
set the data of the bean and then add to the session.
session.setAttribute("bean, b);

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

kranthi adari wrote:In a servlet, you cannot use page-directive, Sorry. Please find my steps below to implement <jsp:usebean> equivalent in servlet.

Import the bean using import <bean-path>
and then create a session object
HttpSevletSession sesssion = request.getSession();
Now create a bean object
MyBean b = new MyBean();
set the data of the bean and then add to the session.
session.setAttribute("bean, b);



This makes the assumption that the variable does not already exist in the scope defined. The jsp:useBean first checks to see if the variable does not exist before making the new object. The most similar code would be:

That assumes the scope being looked at is the session, replace the intended scope as needed.


Steve
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410





I assume you meant


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

Ben Souther wrote:




I assume you meant


Doh, yeah.
C Vivek
Ranch Hand

Joined: Oct 03, 2004
Posts: 115
Thanks guys!
 
jQuery in Action, 2nd edition
 
subject: Servlet equivalent to <jsp:useBean ../>
 
Similar Threads
what's wrong with 'AbstractClass', if object is there
Getting today's date in Facelets
cewolf charts
Trouble with the term "Standard Action"
(URGENT) Singletons and JSP pages