| 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: 56168
|
|
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.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About 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: 56168
|
|
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: 3027
|
|
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: 3027
|
|
Ben Souther wrote:
I assume you meant
Doh, yeah.
|
 |
C Vivek
Ranch Hand
Joined: Oct 03, 2004
Posts: 115
|
|
|
Thanks guys!
|
 |
 |
|
|
subject: Servlet equivalent to <jsp:useBean ../>
|
|
|