File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Assigning String object to bean class reference variable/jsp:usebean issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Assigning String object to bean class reference variable/jsp:usebean issue " Watch "Assigning String object to bean class reference variable/jsp:usebean issue " New topic
Author

Assigning String object to bean class reference variable/jsp:usebean issue

Kaverappa Prabhakar
Ranch Hand

Joined: Aug 18, 2009
Posts: 43
I have two Jsp pages as illustrated below:

page1.jsp



page2.jsp


// The following code does not compile saying that incompatible types(assignment)



The actual error message is: Error(15,9): incompatible types; found: class java.lang.String, required: class gPackage.gBean


// Instead I tried the followingcodes but the first code below compiles and the second code does not compile saying that InterestAmount not found in
java.lang.String )




*Observations:*

I do not want to use "useBean" in page2.jsp because it creates new instanc of the bean per request (the new bean ID will point to different reference
variable (bean id) than the instance created in the pag1.jsp.

The use of session scope messes up the computed values from various requests. Since application computes rates of each loan it is not apporpriate [code] [
to use session scope.

The use of jsp:getProperty won't be helpful because it requires usebean declared in the page2.jsp. Once we do that it crates new instance which does not point to
instance crated in page1.jsp.

*Questions:*

1. How to make it compatiablity? In other words, how to convert string to user defined type?

2. Lets say I use the usebean in page2.jsp with the same scope specified in the first page but without its setProperty code, how to tell this usebean not to create an
instance on this page2.jsp but use its bean ID to fetch values of property in the bean of that instance.

Thanks in advance
Prabhakar
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

Read: UseCodeTags


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1731
    
  25

Hi Kaverappa,

These are my answers:

  • Q1: request.getParameter("gpBean") evaluates to a String. How does your gPackage.gBean look like?
  • Q2: You can't use "page" as a scope, but if you use "request" it will be possible


  • Regards,
    Frits
    Kaverappa Prabhakar
    Ranch Hand

    Joined: Aug 18, 2009
    Posts: 43
    Thanks for your questions:

    Your Q1: here is how my partial gBean looks like


    Your Q2: I am using request as scope.



    I tired casting but does not compile. Also, if assignment of string and user defined type does not work, Is there a way we can make gBean not to created an instance for a given situation.
    Kaverappa Prabhakar
    Ranch Hand

    Joined: Aug 18, 2009
    Posts: 43
    Hello,

    I sent answers to two of your questions:


    Your Q 1: My answer: I sent you the code of the bean to give you an idea how it looks like.

    Your Q 2: My answer: Yes, scope of the bean is Request.


    This project has deadline of coming Tuesday. Would you let me know how to make it compatible? or how make jsp::usebean not to create an instance on a certain situation.

    Your efforts would be greatly appreciated.

    Thanks,
    Prabhakar
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61761
        
      67

    If you want help from some of the more knowledgeable volunteers on this site, you are going to have to post your code in a readable format. That means using code tags as you have been asked to do over three times already.
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender

    Joined: Apr 07, 2010
    Posts: 1731
        
      25

    I don't know exactly what you want to do, but let me try to give you some hints:

    This code won't work because a gpBean is an Object and you want to asign a String to it. Request parameters are Strings. The parameter you are putting inside your forward statement is not forwarding the object but a String instead (the toString() method is called)

    Are you trying to get hold of the bean you instantiated in your page1.jsp?
    If that is what you want you can do it like this:

    It won't instantiate another object with that name as it already exists in the request scope (you have a new page scope, but the same request scope when forwarding)

    What do you want do do with that bean in page2.jsp? It looks like you have not set any properties ?
    Regards,
    Frits
    Kaverappa Prabhakar
    Ranch Hand

    Joined: Aug 18, 2009
    Posts: 43
    I really do appreciate your prompt answer to the posted question.

    The answer to your question "Are you trying to get hold of the bean you instantiated in your page1.jsp? " is YES. That is exactly I wanted to do. Since jsp:usebean placed in other pages was creating a different bean instance I was not able to get the values of bean associated with bean nstance created in page2.jsp. One other point is that I wanted only the bean code in the page1.jsp to create a bean instance and the bean code in the rest of the pages without setProperty to give me access to bean instance created in page1.jsp via beanId. The other reason I did this was because I was getting static/nonstatic access compilation error message when I tried to acces instance variables and non-static methods in gBean and gClass. To get arround this problem I places jsp:useBean in every other page without setPropery. I also accessed gClass via gBean instance ID of page1.jsp.

    The page2.jsp displays at the top of the page values from the gBean properties pertaining to the bean instantiated in pag1.jsp. The rest of the page2.jsp displays to the user input fields (dynamically created onLoad) depending on what values user entered and posted via pag1.jsp.(The values entered by the user on page2.jsp is associated with the same bean instantiated in page1.jsp)

    The page2.jsp has a submit button. When the user submits page2.jsp the values of input field values of this page gets posted to a different Jsp:usebean via page page3.jsp. The page3.jsp just instantiates a different bean shown in the code below and calls a method in a different class and forwards a page4.jsp which displays final (summary) result.

    The page3.jsp also has the same jsp:usebean code of page2.jsp as shown below in the code.This is because data entered by the user in page2.jsp (associated with the same bean instantiated in page1.jsp) is required in page3.jsp to do the process (to make dicision) to call a specific method in a different class and forwad a summary page (page4.jsp).

    The page4.jsp uses the values from the property of the bean instantiated in page1.jsp and the values computed (associates with the same bean instantiated in page1.jsp) by a method in a class after a call from page3.jsp

    I made changes as suggested by you and ran it. It did get hold of the bean instantiated in page1.jsp and displayed and made available correct values in page2.jsp from its properties.

    But I get run time stack overflow error shown below when it encounters in page3.jsp at the following code

    I am sorry that I did not talk about page3.jsp and page4.jsp for two reasons;

    1. Thought that the fix made between page1 to page2 would apply to the fix that might need between page2 to page3 to page4.

    2. I did not want to make question more complex by writing too much. It turns out that I was wrong I should have given the same kind of details as this one. I
    apologize for my mistake.


    I was thinking that the Submit button in pag3.jsp changes the dynamics and therefore encountering the stack over flow error in page3.jsp at

    I am learning on my own and no training is provided to me to learn new technology. My supervisor is Economics and he gives deadline without providing me the proper training and understanding the complexity of this technology. There are only three of us. One deals just the management and the other is FORTRAN person. There is no one knowledgeable to discuss in this area. I have been given warning about this project if I don't meet the deadline. It is unlikely He wanted me to fix it by Monday but I told him last Friday that I will try to fix it by Tuesday. He did not agree but I can only asks on Monday for few more days.

    Please bear with me about this question.

    Thanks,
    Prabhakar
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender

    Joined: Apr 07, 2010
    Posts: 1731
        
      25

    Kaverappa,

    You are getting the StackOverflowError because you are adding elements to a List in some sort of infinite loop. I can't see from your code where you are doing that but check the methods you are calling in page3.jsp.

    If you are using Tomcat as a server you could opt for writing some lines to the Console window to check how far you get in page3.jsp before getting the error. Like this:

    Regards,
    Frits
    Kaverappa Prabhakar
    Ranch Hand

    Joined: Aug 18, 2009
    Posts: 43
    I placed System.out.println code as shown in the code at the end of this message. The execution does pass "before instantiating userPmtBean" and “after instantiating userPmtBean" but does not reach "after executing gBean ".

    Here is the actual output out of Oracle JDeveloper tool:

    10/05/03 19:26:10 in page3.jsp before instantiating userPmtBean
    10/05/03 19:26:10 after instantiating userPmtBean

    I also tried the following steps:

    1. Removed all lines of code below gBean statement on page3.jsp but I still get the stackoverflowError.
    2. I added beanName (“gBean”) but still get the stackoverflowError. (Is gBean is the beanName?) I hope I used that correctly. If not please let me know.
    3. Removed beanName and changed type = "gpPackage.gBean" to class = "gpPackage.gBean" with this change, stackoverflowError disappeared but gBean points to a different bean instance.

    You had indicated in your previous update, if I understood correctly, that it would work as long as the page is forwarded within the request scope and with Type option. Page2.jsp has a submit button and as part of form action it displays page3.jsp. You are correct since page2.jsp invoked via action(not forwarded) page3.jsp .

    So, the question now is: how to get hold of bean instance of page1.jsp in page3.jsp?



    Thanks for your patience ,
    Prabhakar
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender

    Joined: Apr 07, 2010
    Posts: 1731
        
      25

    Kaverappa,

    When you submit a form you are creating a new request/response pair, so there is no way to get to the previous request. There are two options remaining:
  • use the session as a scope in page1.jsp
  • submit all the properties of your bean as hidden properties in page2.jsp

  • I still don't know why your server is coming with a StackOverflowError because the line on page3.jsp with:

    should lead to an error like:
    ERROR bean gBean not found within scope

    Try to focus on getting the flow right first:
  • set only a single property with a setProperty in page1.jsp, and try to get to page4.jsp
  • do the calculations later, adding them one by one

  • Regards.
    Frits
    Kaverappa Prabhakar
    Ranch Hand

    Joined: Aug 18, 2009
    Posts: 43
    Frits,

    I appreciate your reply and guidance.

    Here is the brief summary of how this application works:

    pricingUser.html

    In this user(borrower of loan) enters the input field values of loan.

    clicks on submit button

    Upon submiting, this page invokes page1.jsp

    Page1.jsp

    This page post the input values of pricing.html to the gBean and calls method in gClass.
    The gClass method does the calculation and determines the number of payments required to pay off
    the loan based on input values entered in pricing.html.

    After returning from the method this page forwards page2.jsp.

    page2.jsp

    This page creates dynamically (see on load tag of body of page2.jsp) input fields. The number of
    input fields created depends on the number of payments computed (as described under page2.jsp
    above.)

    The user(borrower) has to enter the dollar amount in each of the displyed input fields

    As you can we cannot post all input field values in one submit action. ( We have have two separate submits: one in pricingUser.html and one in page2.jsp). It uses page1.jsp, page2.jsp, page3.jsp and page4.jsp. I have three other functions in the same application whch requires only one submit button (in pricingSys.html. It uses page1.jsp and page4.jsp) These three functions works fine with request as scope but messes up.(see below for details)

    Since user(borrower) has to enter dollar amount for each of the input payment fields we cannot
    make it hidden on this page.

    I was using session scope before changed over to request scope. I did this because it was messing
    up the values within the session. Lets say I am processing pricing function and while it is still
    running and/or just compeleted processing but I don't close the function (last page page4.jsp still
    on diplay) and ran the pricing function again (simultaneously) While doing so the result output of one
    mixes with the other one. So to get arround this problem I used request as scope. I don't know other
    fancy way to get arround this mixing/cross over problem. The easiest way for me to get arround the
    mixing/cross over problem was to change the scope from session to request.

    Because of the above facts, I thought of the following:


    1. Pass the gBean from page2.jsp to page3.jsp as a form input field(hidden) or part of URL and
    extract it in page3.jsp and create class object from string gBean, perhaps as below: I prefer not
    to use URL to pass because gBean has memory reference address (If passing URL is the easy way to
    pass, I will use it as temporary fix.)


    The code for creating object was borrowed from a University website:
    http://www.rgagnon.com/javadetails/java-0351.html


    I tried to run that as a stand alone code but it encounters some kind of problem at
    the following code.

    If you can make the attached standalone code to work that would be of great help.

    2. To my understanding once the form action takes place (after click on submit button) in
    page2.jsp the control moves to page3.jsp and there is no way to make the control to get back to
    the point in page2.jsp following the submit button code (in page2.jsp). If it is possible I can
    remove all lines of code below jsp:useBean in pag3.jsp and move them ot new page3a.jsp. In
    otherwords only the following code will be in page3.jsp and the rest moved to new page3a.jsp.

    Now this new page3a.jsp has to be somehow forwarded from page2.jsp at a point below the submit
    button in page2.jsp. I think it not possible to use this approach to get arround the problem.


    Here is summary of my questions:

    1. Would you mind in looking in to it to figure out what the problem at the code line of CreateObject indicated above?

    2. Do think it will work if we pass gBean from page2.jsp to page3.jsp and convert passed string gBean to gBean class object?

    3. What is that I doing wrong in the following code that gives me null value of passed parameters.


    It will be of great help if you can provide me the answer to the above three questions.

    Thanks again

    Prabhakar



    Kaverappa Prabhakar
    Ranch Hand

    Joined: Aug 18, 2009
    Posts: 43
    Frist,

    One more update. I was able to run Create Object code in the modified form like shown below:



    But the above code creates String Object and not the User Defined Class Object. So this won't be of any help in terms of clearing Error(15,5): incompatible types; found: class java.lang.Object, required: class genPricingPackage.genPricingBean.

    I want to add two more question to the set of questions sent in my previous update.

    Q 4: If I use the inheritance like the following, will it enable use to retrieve the bean instance inpage1.jsp using some special techniques ?



    Q 5: Is it possible to iterate over gBean instances (I I am storing gBean instances -reference variable values and the gClass instances in gBean class) using HashMat by trying to match with the gBean value (String) passed by page2.jsp to Page3.jsp and if it matches extract the gBean instance and use it inpage3.jsp?

    I wrote a method to do the above but encountered the following problem:

    The problem with the above code is that the method has to be Static to access from page3.jsp. If I make the method static then I won't be able to link the gBean instance with the assicated gClass instance which is critical to identify the pair.
    This is because I won't be able to compile the method since non-static variable can not accessed from the static method. If I make this method non-static I won't be able to access the method since from JSP one can only access static method without a variable refernce. If I create a referance variable with new facility in page3.jsp it will point to a different instance.

    Any suggestion to get around with the above problem? Will it require an interface class/method?

    Prabhakar

    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender

    Joined: Apr 07, 2010
    Posts: 1731
        
      25

    Kaverappa,

    Here some answers to your questions:
  • Hidden parameters are just like form parameters: they are Strings; hence you can't pass your gBean object
  • You can't call the toString() method on an Object and reconstruct the Object on another page: that is done with serialization and deserialization, and to complicated as a solution to your problem

  • The Session is not good for your problem because you are using the name "gBean" as a key to your object. If you have two client requests both will use the same name and they will both write to the same object. What you can do is: use a key that is unique for each request. So your Session object will have multiple instances of your gBean object, accessed by different keys. Just some thoughts here:
  • use a field that is unique for each request, for example a String containing the current date and time
  • create a counter in the session and give every request an unique number, so you increase it with every request (be careful here: use synchronized methods to read and update the counter, so you don't encounter multithreading problems again)

  • But the solution I was thinking of (without using the Session object) was to put hidden parameters in the form of page2.jsp for all the properties of your gBean, for instance:

    In page3.jsp you can instantiate a new gbean object and set all its properties with the hidden parameters and the user fields of page2.jsp. As you are using post as a method you can submit almost an unlimited number of parameters.

    I hope this gives you some idea of how to proceed.
    Regards,
    Frits
    Kaverappa Prabhakar
    Ranch Hand

    Joined: Aug 18, 2009
    Posts: 43
    Frits,

    Thanks a lot for your answers and two convincing ideas that will work. Since I am knew to this stuff aI will stay with request scope processing and I will learn about session version later.

    Fortunately I managed to convince my employer that I will implement 3 of the six functions of the application/project by Thursday and the rest of the 3 functions later. The first three functions system generates number of payments and the payment amount and most of the time user uses these three functions and are called system generated pricing. These system generated functions requires only one submit and uses page1.jsp and the page4.jsp. I have kept these functions ready to implement.

    The other 3 functions generate number of payments due but not the payment amount and these require two submits. I got hung up on these functions.
    I understood your second idea very well and now I will have rest of the week days and week end to complete the rest of the three similar functions. With the ideas you gave me I will be able to complete this task.

    I will let you know my progress on this.

    Many thanks,
    Prabhakar
    Frits Walraven
    Creator of Enthuware JWS+ V6
    Bartender

    Joined: Apr 07, 2010
    Posts: 1731
        
      25

    Hi Kaverappa,

    You are welcome and good luck!

    Regards,
    Frits
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Assigning String object to bean class reference variable/jsp:usebean issue