Jsp:useBean and assigning a value to that bean

Hello,
I have a type called Country and I have declared a bean using the "jsp:useBean" syntax.
I then try to assign a value to that bean in a scriptlet. Eventually I try to retrieve the values using "jsp:getProperty". The problem is that it appears to null.
Here is the code of the jsp
<%@ page language="java" %>
<%@ page import="com.parispano.latinamericaguide.*" %>
<%
/* We retrieve the Countries object from the servlet context. */
ServletContext sc = getServletContext();
Countries cs = (Countries)sc.getAttribute("countries");
%>
<jsp:useBean id="country" class="com.parispano.latinamericaguide.Country"/>
<%
/* If the request contains a country id, then we set the variable. */
country = cs.getCountryFromId(Integer.parseInt(request.getParameter("countryID")));
%>
<html>
<head>
<title>Country details</title>
</head>
<body>
<table>
<tr><td>Name</td><td><jsp:getProperty name="country" property="name"/><!-- This is not working--></td></tr>
<table>
<%
out.println(country.getName());//This is working
%>
</body>
</html>This is the code for the bean
package com.parispano.latinamericaguide;
* @author Julien Martin
* Represents a country.
public class Country implements Comparable {
     private int id;
     private String name;
     private int landArea;
     private String capital;
     private String currency;
     private String flag;
     private String internet_domain;
     private String dialling_code;
     private float literacy;
     private float male_life_expectancy;
     private float female_life_expectancy;
     private String map;
     private int population;
     private int population_year;
     private float birth_rate;
     private int birth_rate_year;
     private float death_rate;
     private int death_rate_year;
     private int gdp;
     private int gdp_per_head;
     private int gdp_year;
     private float unemployment_rate;
     private int unemployment_rate_year;
     public Country() {
     public Country(
          int id,
          String name,
          int landArea,
          String capital,
          String currency,
          String flag,
          String internet_domain,
          String dialling_code,
          float literacy,
          float male_life_expectancy,
          float female_life_expectancy,
          String map,
          int population,
          int population_year,
          float birth_rate,
          int birth_rate_year,
          float death_rate,
          int death_year_rate,
          int gdp,
          int gdp_year,
          float unemployment_rate,
          int unemployment_rate_year) {
          this.id = id;
          this.name = name;
          this.landArea = landArea;
          this.capital = capital;
          this.currency = currency;
          this.flag = flag;
          this.internet_domain = internet_domain;
          this.dialling_code = dialling_code;
          this.literacy = literacy;
          this.male_life_expectancy = male_life_expectancy;
          this.female_life_expectancy = female_life_expectancy;
          this.map = map;
          this.population = population;
          this.population_year = population_year;
          this.birth_rate = birth_rate;
          this.birth_rate_year = birth_rate_year;
          this.death_rate = death_rate;
          this.death_rate_year = death_year_rate;
          this.gdp = gdp;
          this.gdp_year = gdp_year;
          this.unemployment_rate = unemployment_rate;
          this.unemployment_rate_year = unemployment_rate_year;
     public float getBirth_rate() {
          return birth_rate;
     public int getBirth_rate_year() {
          return birth_rate_year;
     public String getCapital() {
          return capital;
     public String getCurrency() {
          return currency;
     public float getDeath_rate() {
          return death_rate;
     public int getDeath_rate_year() {
          return death_rate_year;
     public String getDialling_code() {
          return dialling_code;
     public float getFemale_life_expectancy() {
          return female_life_expectancy;
     public String getFlag() {
          return flag;
     public int getGdp() {
          return gdp;
     public int getGdp_per_head() {
          return gdp / population;
     public int getGdp_year() {
          return gdp_year;
     public int getId() {
          return id;
     public String getInternet_domain() {
          return internet_domain;
     public float getLiteracy() {
          return literacy;
     public float getMale_life_expectancy() {
          return male_life_expectancy;
     public String getMap() {
          return map;
     public String getName() {
          return name;
     public int getPopulation() {
          return population;
     public int getPopulation_year() {
          return population_year;
     public int getLandArea() {
          return landArea;
     public float getUnemployment_rate() {
          return unemployment_rate;
     public int getUnemployment_rate_year() {
          return unemployment_rate_year;
     public void setBirth_rate(float f) {
          birth_rate = f;
     public void setBirth_rate_year(int i) {
          birth_rate_year = i;
     public void setCapital(String string) {
          capital = string;
     public void setCurrency(String string) {
          currency = string;
     public void setDeath_rate(float f) {
          death_rate = f;
     public void setDeath_rate_year(int i) {
          death_rate_year = i;
     public void setDialling_code(String string) {
          dialling_code = string;
     public void setFemale_life_expectancy(float f) {
          female_life_expectancy = f;
     public void setFlag(String string) {
          flag = string;
     public void setGdp(int i) {
          gdp = i;
     public void setGdp_per_head(int i) {
          gdp_per_head = gdp/population;
     public void setGdp_year(int i) {
          gdp_year = i;
     public void setId(int i) {
          id = i;
     public void setInternet_domain(String string) {
          internet_domain = string;
     public void setLiteracy(float f) {
          literacy = f;
     public void setMale_life_expectancy(float f) {
          male_life_expectancy = f;
     public void setMap(String string) {
          map = string;
     public void setName(String string) {
          name = string;
     public void setPopulation(int i) {
          population = i;
     public void setPopulation_year(int i) {
          population_year = i;
     public void setlandArea(int i) {
          landArea = i;
     public void setUnemployement_rate(float f) {
          unemployment_rate = f;
     public void setUnemployment_rate_year(int i) {
          unemployment_rate_year = i;
     public String toString() {
          return name;
     public int compareTo(Object o) {
          Country c = (Country)o;
          int cmp = name.compareTo(c.name);
          System.out.println(cmp);
          return cmp;
}Can anyone please help?
Thanks in advance,
Julien.

Thanks,
I have added scope="application". It still show "null" indicating that the bean is not initialized with the properties. I would like a quicker way to initialize a bean property than invoking the setProperty tag for each property.
Here is a simple example that demonstrates the problem:
<%@ page language="java" %>
<%@ page import="com.tests.*" %>
<jsp:useBean id="monBean" class="com.tests.MonBean" scope="application"/>
<%
MonBean mb = new MonBean();
mb.setName("toto");
monBean= mb;
%>
<html>
<head>
<title>Lomboz JSP</title>
</head>
<body bgcolor="#FFFFFF">
<jsp:getProperty name="monBean" property="name"/>
</body>
</html>And the bean
package com.tests;
public class MonBean {
private String name;
public String getName() {
     return name;
public void setName(String name) {
     this.name = name;
}This show "null".
Any other idea why this is not working?
Thanks,
Julien

Similar Messages

  • Is there any difference between "jsp:useBean" and "scriptlet" ?

    A few days ago, I asked similar question. But I didn't get the answer I wanted.
    I want to know the differnce between <jsp:useBean../> and <% .. %>(scriptlet).
    I tested in three environments(Oracle Jserv, OC4J, and Apache Tomcat).
    In Oracle Jserv and OC4J, a problem occured. But, Apache Tomcat does not occur a problem.
    For example,
    1) TestClass.java (Bean)
    public class TestClass {
    private String txt;
    public class TestClass {
    txt = "Test"; ----- (g
    public String getTxt() {
    return txt;
    2) test.jsp
    <html><body>
    <% TestClass test = new TestClass(); %> ---(h
    <%= test.getTxt() %>
    </body></html>
    Assume that I visit "http://localhost:8888/test.jsp".
    In Tomcat, if I change "Test"(number(g) to "Test1" and compile the browser shows the change.
    But Oracle Jserv and OC4J does not do that. They also show the old String.
    So, I changed <% TestClass test = new TestClass(); %>(number(h) to <jsp:useBean id="test" class="TestClass" />. That is, I changed "Scriptlet" to "JSP useBean Tag".
    Then, Oracle Jserv and OC4J also show the changes.
    To conclude, is there any difference between "JSP useBean Tag" and "Scriptlet"?
    Can't I use a scriptlet (to make a class) in Oracle Servlet Engine?
    Thanks.

    It could be as simple as the JSP not recompiling between java recompiles - ie, it compiles in the link to the old class.
    Try changing the JSP file (add and delete a space) after you change the java code, and then see what happens.
    Jonny
    null

  • SUBMIT and return the value of that report into internal table

    Dear all,
    I have a requirement. I want to submit a report and return the value of that report into my internal table.
    How to do this.
    Pl. guide.

    Hi Vidhya,
    Below links from SAP help will resolve your issue.
    http://help.sap.com/saphelp_nw04/Helpdata/EN/fc/eb3bde358411d1829f0000e829fbfe/content.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/fc/eb3bd1358411d1829f0000e829fbfe/content.htm
    Edited by: Harsh Bhalla on Jan 2, 2010 11:54 AM

  • Jsp:useBean and definition of bean inside jsp:declaration

    I use the following code where I define a bean in jsp:declaration
    Its name is localBean. the problem is that jsp:useBean causes
    a crash and my page is not working without understanding why.
    When I replace the localBean in the jsp:useBean with another Bean
    which is an actual class that resides in the system I have not a problem.
    Any suggestions???
    <HTML>
         <jsp:declaration>          
              static public class localBean {
                   private String value;
                   public String getValue() {
                        return value;
                   public void setValue(String value) {
                        this.value = value;
         </jsp:declaration>
         <jsp:useBean id="localBean" scope="page" class="localBean">
              <jsp:setProperty name="localBean" property="value" value="World"/>
         </jsp:useBean>
         <%-- Whatever HTTP parameters we have, try to set
              an analogous bean property -- %>          
         <jsp:setProperty name="localBean" property="*"/>
         <head>
              <title>Hello World JavaBean</title>
         </head>
         <body>
              <center>
                   <p><h1>
                        Hello
                        <jsp:getProperty name="localBean" property="value"/>
                   </h1></p>
                   <form method="post">
                        Enter a name to be greeted:
                        <input type="text" size="32" name="value"
                        value="<jsp:getProperty name='localBean' property='value'/>">
                        <br>
                        <input type="submit" value="submit"/>
                   </form>
              </center>
         </body>
    </html>          

    Thanks,
    I have added scope="application". It still show "null" indicating that the bean is not initialized with the properties. I would like a quicker way to initialize a bean property than invoking the setProperty tag for each property.
    Here is a simple example that demonstrates the problem:
    <%@ page language="java" %>
    <%@ page import="com.tests.*" %>
    <jsp:useBean id="monBean" class="com.tests.MonBean" scope="application"/>
    <%
    MonBean mb = new MonBean();
    mb.setName("toto");
    monBean= mb;
    %>
    <html>
    <head>
    <title>Lomboz JSP</title>
    </head>
    <body bgcolor="#FFFFFF">
    <jsp:getProperty name="monBean" property="name"/>
    </body>
    </html>And the bean
    package com.tests;
    public class MonBean {
    private String name;
    public String getName() {
         return name;
    public void setName(String name) {
         this.name = name;
    }This show "null".
    Any other idea why this is not working?
    Thanks,
    Julien

  • Is it possible to read the contents of an Excel cell in DIAdem and assign its value to a variable in a VBS script.

    Hi All,
    Initially I thought this little problem would be relatively straight forward but now I’m not so sure. I am familiar with the mechanism by which DIAdem communicates with Excel and how to change the contents of a cell via a VBS script. In my task the contents of the cell in the first row, first column of MyProblem.xls contains the text “DIAdem”. I would like to be able to read this value and assign it to the variable MyString. I originally thought of doing something simple like this:
    Dim MyString
    Dim Excel, ExcelSheet
    Set Excel = CreateObject(“Excel.Application”)
    Excel.Workbooks.Open(“C\MyProblem.xls”)
    Set ExcelSheet = Excel.Workbooks(“MyProblem.xls”).Sheets(“Sheet1”)
    MyString = ExcelSheet.Cells(1,1)
    At this point I would have hoped that MyString would have been set equal to “DIAdem” and I could have used MyString to change the name of a channel in the data portal if I desired using the following code:
    Data.Root.ChannelGroups(1).Channels(1).Name = MyString
    Doesn’t seem to work though. I’m guessing it is because MyString has not picked up the value of the contents of the cell? Can anybody propose a solution to my problem or indeed confirm whether what I am proposing to do is technically feasible.
    Thanks in advance for any responses.
    Matthew

    Hi Matthew,
    Just staring at your ActiveX code, it looks fine to me.  My first thought is that this should work as you outlined it, and I've done this sort of thing many times, so I know it can work.  My second thought though, is that what you probably really want is a DataPlugin and not a VBScript.  Then you could just drag&drop the Excel file into the Data Portal and load all the properties and channels you want from the Excel file.  If you have DIAdem 2010 or later you can use the SpreadSheet reader object in the DataPlugin to avoid the Excel ActiveX functions (and Excel's jealously with other applications trying to read a file it has open already).
    Feel free to send me a few sample Excel files and describe what you want to load from the various cells, and I'd be happy to help you get a DataPlugin written to load your data.  You can also email me at [email protected]
    Brad Turpin
    DIAdem Product Support Engineer
    National Instruments

  • Jsp:usebean How to get value of an object from a bean

    I am new to JSP and am struggling from past few days to solve a problem. Consider the below scenario
    Class Book{
    private String author;
    public void setAuthor(String author)
    public String getAuthor(){
    return author; }
    Class Rack{
    private Book []books;
    public []Book getbooks()
    return books;
    public void setbooks([]Book val){....}
    Suppose Books gets populated only using Rack and I have Rack populate in my session, using jsp:usebean or any other tag how can I get value of author? Does using tag handle null value check of books, if books are not set by any chance?
    Regards,
    Dhyan

    I got a way out to access the attribute, i feel it is
    <jsp:usebean id="myRack" class="settings.Rack"/>
    <c:foreach var="book" item="myRack.books">
    </c:foreach>
    I am not able to check if this is correct or not because myRack.books is not having any value set by me in the request scope. How do i get instance of Rack class set by another page in the current request? I can get the value if i use scriptlet but i dont want to use scriptlet.
    I am continiously trying, if I get an answer, I shall post, else pls somebody guide.

  • Istore 11.5.10 - creating a new site and assigning existing users to that

    Hi,
    I need to create a new site in iStore and assign all the Canada customers to that site. Can you please let me know how to do this.
    I though that I would create a custom responsibility ABC_IBE_CUSTOMER same as IBE_CUSTOMER and attach that responsibility to the new site and the same responsibility to all the canada custoemers and when they login they will directly been directed to the new site in the customer UI. But it did not happen like that. I might be missing many things inbetween. Can you please let me know the step by step process to achieve this.
    Thanks,
    Srikanth

    function(){return A.apply(null,[this].concat($A(arguments)))}
    Why adobe has not issued an update!!!.
    Agreed. I'm disappointed that no patch has yet been issued. Hope we don't have to go on explaining this for the next 12 months.

  • Jsp:useBean and Tomcat

    Hi!
    I have installed Tomcat as localhost and succeed in running scriplets. But now I want to try using jsp:useBean
    I have compiled a simple class, test1.class and placed it under webapps/root together with the jsp-file.
    When I use �
    <jsp:useBean id="test1" scope="page" class="test1"/>
    to start test1, Tomcat tries to find it in org.apache.jsp
    Then I tried to add - package myFolder; - in test1 but it won�t work.
    So! These are my questions.
    Is it possible for Tomcat to find a class placed in the webapps/root folder?
    Where can I find org/apache/jsp, is it in a .jar-file somewhere? I can�t fint it when I use - search files/folders.
    Where should I place test1.class and how do I write the useBean-tag to make it work?
    Thanks for any answer
    Regards Peter

    your beans should always be under /web-inf/classes
    so if you want to put it in the root directory it should look like this
    tomcat-path/webapps/root/web-inf/classes
    in your jsp file, don't forget to import the bean i.e
    <%@ page import="package_name.mybeans.*" %>
    then use the jsb:useBean the way you did
    <jsp:useBean id="test1" class="test1" scope="page" />
    it should then work fine,
    regards

  • jsp:useBean... didn't create new bean?

    According to tutorial or specification (in my perception),
    the following tag will create a new bean if it doesn't
    exist:
        <jsp:useBean id="prodDetail"
            class="mycomp.model.ProductDetail"
            scope="request" />Say I put the tag in Product.jsp, and it is an entry form
    for insert and update a new product. For insert, I intend
    to access Product.jsp directly from browser. For update,
    I have clients to access a servlet that in turn, will
    dispatch {[Product.jsp + prodDetail to be updated)}.
    There was no problem with update, however, for insert
    (directly access Product.jsp via url), I got this exception.
    I am using Tomcat, and I wonder whether it's a "Java error"
    or "Tomcat error".
    Any help would be appreciated. Thanks...
    javax.servlet.ServletException
         at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:459)
         at _0002fProduct_0002ejspProduct_jsp_0._jspService(_0002fProduct_0002ejspProduct_jsp_0.java:187)
         at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
    ...etc...
    Root cause:
    java.lang.NullPointerException
         at _0002fProduct_0002ejspProduct_jsp_0._jspService(_0002fProduct_0002ejspProduct_jsp_0.java:112)
         at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:119)
         at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
         at org.apache.jasper.servlet.JspServlet$JspCountedServlet.service(JspServlet.java:130)
    ...etc...

    Whoops, sorry, I made a silly silly mistake, apparently
    there was an extra scriplet <% } %> I forgot to delete.
    It's works now....
    I figured that out when I was composing following posting
    about snipplet of the JSP..., so I guess this "embarassing"
    posting does help me too...., in a short time :D

  • How does "Unflatten From String" take a type and return a value of that type?

    http://zone.ni.com/reference/en-XX/help/371361E-01/glang/unflatten_from_string/
    How exactly does the "type" argument for "Unflatten From String" work? I need to create a VI that takes a type, passes it as an argument to several calls of the "Unflatten From String" function, and returns an array containing elements of the type originally passed. The "Unflatten From String" function seems to do some magic though, because the type of the "value" that it outputs changes depending on the type it is passed as input. How do I do the same magic in my VI?
    Ultimately, what I need to accomplish is an unflatten-list operation. Given a type T and a byte string of length L (which contains a concatenation of T elements that are flattened to their bytes), create a VI that unflattens all the types in the string and return an array of length (L / sizeof(T)) that contains each type.
    Note: performing the unflatten-list operation is trivial, but I cannot for the life of me figure out how to do it in a VI that takes a type and returns an array of the appropriate type. By the way, my data is being given to me from another source, so please don't bother suggesting that I should be flattening an array using LabVIEW's "Flatten To String" function in the first place. My data is not given in LabVIEW's array format: http://zone.ni.com/reference/en-XX/help/371361B-01/lvconcepts/flattened_data/
    Thanks a ton!
    -Wakka

    Take a look at this example:  You can see that the flattened string contains several bytes.  The first four bytes contain the length of array (number of elements).  Since the data type is U32, the next 32 bits (4 bytes) contains the value of the first element, and so on.  Could you possibly use this scheme to do what you want to do?  Other data types present different outputs.  You would have to experiment with them.
    - tbob
    Inventor of the WORM Global

  • Querying and Assigning Date Value

    Hello, this query is not working. I am trying to assign the date value to the from_date variable.
    set result_list [execsql "select TCM_DATE_BEGIN_REQD into: TO_CHAR(from_date, 'YYYY/MM/DD') from tcm_tau_component where tcm_reference_number = $tau_reference_number" ]
    This is the error I get:
    The following bind variable(s) are not defined:
    Please help.

    I don't know what language this is but you're trying to select into a format mask. That looks suspicious. Try this:
    select TO_CHAR(TCM_DATE_BEGIN_REQD, 'YYYY/MM/DD') into :from_date ...

  • Splitting string separated by commas into words and assigning numerical values

    Project details :
    I have a project where a description field is being looked at and based on the values (multiple words) I have to do a vlookup from another spreadsheet to get the dates from the appropriate date fields.
    I have created a user defined function to extract keywords from a list from a table. It extracts words from the string separated by comma delimiter.
        I am thinking of assigning values something like this: cooler = 1, compressor = 2, frame = 2, change of order = b etc.
        So if there is a “cooler, compressor, frame, change of order” in the cell, then it should read as: b12 (unique values). Based on the values, I can do a vlookup on the dates. I tried sumproduct in conjuction with search function but did not
    work. Tried split function but did not quite work.
    Any help regarding this is greatly appreciated. Thank you, regards
    keywords
    engine, compressor, frame, cooler     =Keywords(Wordlist,A2) is the function I am calling
    frame, cooler
    engine, cooler, change order
    cooler

    Hi,
    You're asking this question in the wrong forum, this is a PowerShell forum.
    The Excel VBA forum is this way:
    http://social.msdn.microsoft.com/Forums/en-us/home?forum=exceldev&filter=alltypes&sort=lastpostdesc
    Side note to whoever voted up the OP - why??
    Don't retire TechNet! -
    (Don't give up yet - 12,950+ strong and growing)

  • JSP UseBean setproperty work  deleted value setting work around

              I was using the set property * and i had to navigate through the pages. I was stuck with when some one deletes the value and setting of is not being done. The only way i could find is to set " " a space onSubmit or unLoad and finally while setting into a database or anything make a trim of it .
              If any one finds a good way of over coming this please mail me at [email protected]
              Waiting for a response.
              Thanx in advance.
              Lakshmi Narayan.
              

              I was using the set property * and i had to navigate through the pages. I was stuck with when some one deletes the value and setting of is not being done. The only way i could find is to set " " a space onSubmit or unLoad and finally while setting into a database or anything make a trim of it .
              If any one finds a good way of over coming this please mail me at [email protected].
              Waiting for a response.
              Thanx in advance.
              Lakshmi Narayan.
              

  • JSP useBean and Tomcat NoClassDefFoundError

    Hi,
    I have a webapp on tomcat whose structure is like this
    /tomcat
    --/webapps
    ----/mywebappname
    ------/WEB-INF
    --------/classes
    ----------MyBean.java
    When I use useBean class="MyBean" I get a NoClassDefFoundError for this particular class.
    I suspect this is some kind of classpath issue but do not know how to fix it. Help appreciated.

    place u'r class file in <tomcat-root>/classes folder.
    if there's no such folder ,u'll have to create it.

  • How to assign a value of the bean to a js variable.

    Hi,
    Im getting some values from the DB which is stored in a list , Now the list is sent across to the jsp through the bean.
    I need to get the length of the list
    I tried something like this.
    <script>         
    var size = <% rmaLicenseTransferForm.slfextUiElements.size(); %>
    </script>rmaLicenseTransferForm is the name of the formBean and slfextUiElements is the name of the list.
    I guess this wont work....by the way i using struts framework.....
    Thanks

    Use the JSTL fn:length tag.

Maybe you are looking for