• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Servlet equivalent to <jsp:useBean ../>

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67750
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator





I assume you meant
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ben Souther wrote:





I assume you meant



Doh, yeah.
 
C Vivek
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys!
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic