File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet equivalent to <jsp:useBean ../>

 
C Vivek
Ranch Hand
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<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
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
kranthi adari
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




I assume you meant
 
Steve Luke
Bartender
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben Souther wrote:




I assume you meant


Doh, yeah.
 
C Vivek
Ranch Hand
Posts: 115
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic