• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

standard action question

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the output at line 6 of the following code fragment? Consider that "postageType" already exists in the session.


1.<jsp:useBean id="postageType" scope="session"
2. class="com.baboon.scwcd.PostageType" >
3. <% postageType.setType("First Class"); %>
4.</jsp:useBean>
5.
6.<%= postageType.getType() %>



what will be thew out put:

1 The output is: First Class
2 The output is: Second Class
3 The output is undetermined.
4 Compile time error.
5 Runtime error.


i have a doubt what and all we can use inside standard actions(scriplet,expressions,el...) what wil be the impact on the code how it wil behave

because the answer they had given for this is.

Answer 3 is correct.

The bean postageType is already present in the session scope, the JSP engine will skip the body of the useBean tag. The scriplet code at line 3 is not executed.The output of the scriplet code at line 6 cannot be predicted, the property has been set somewhere else.

A Java Bean is instantiated with the no-argument constructor in JSP pages, this does not give the Bean developer the opportunity to initialize the bean. If the bean was not present in the session the scriplet code from line 3 would have been executed, and the bean would be inititialized with "First Class".
This mechanism has been introduced to initialize Java Beans in JSP pages.

and every thing for me is confusion...please if any one with detail can help me..


 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mani !

I cannot let you cry like that

First of all, the jsp:useBean tag body-content is set to JSP; it means inside you may have scriptlets, EL, tags and so on (everything you may put in a JSP). Typically, you see scriptlets or jsp:setProperty standard action in its body.

About the body evaluation, the rule is simple:

A jsp:useBean body is evaluated IF AND ONLY IF :
  • the bean is NOT found;
  • a bean is created (using the no-arg constructor (or instantiate method if beanName is used)).


  • If the conditions are satisfied, then the body content is evaluated. In your case, the scriptlet is executed and postageType type is set to First Class.

    But if the bean is found, then the first condition is not satisfied, so following the rule : the body content of the jsp:useBean is NOT evaluated. And as it's said in the question "Consider that "postageType" already exists in the session", so bean is found, so useBean body is not evaluated. This mean you cannot take into consideration the value of the setType method in the body.

    As you don't know what the type value has been set to before, the output of the JSP expression cannot be predicted.

    I hope it's a little bit clearer.
     
    mani kanth
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Frederic ,thanks for wonderful explination really i understood very clearly.once again thanks for your answer and please keep on clarify my doubts what ever i post in this forum (because your answer is really clear)


    I cannot let you cry like that


    yes its true(haha)

    and specially the links you provided are really helpful to me am going through that from past three days.
    thanks and i have doubt should i read spec is it must.
     
    Frederic Esnault
    Ranch Hand
    Posts: 284
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi !

    I'm glad you found it clear.

    For the spec I can help you too.

    Imo, the servlet spec is a MUST READ. You'll find them surprisingly easy to read, and you should go through each chapter and maybe underline the things that seems important to you. I did this work already, you can find my servlet spec summary on my website (downloads and contents).

    For the JSP & JSTL spec, read them from start to end is a bit too much, but you MUST read at least :
  • the standard action description (body content, attribute & attributes combinations;
  • the EL evaluation/coercion/etc spec
  • the core custom tags description (JSTL spec, a must also)


  • This will make some things you found difficult become sooo clear !

    But well, some people passed without reading spec. I think they take a risk, but this is up to them.

    For example, yesterday I finally printed the 500 pages EJB spec (I bought a laser printer just for spec printing ). I'm a little bit afraid of that huge paper stack, but I will definitely read them and try to make a spec summary too (You may use it when you switch to SCBCD ).

    Thanks again for so nice words !
     
    mani kanth
    Ranch Hand
    Posts: 57
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    hi Frederic,

    am so much thankful too you i already downloaded servlet spec link of yours.And i need to ask you one more thing like Servlet Spec Summary do you for jsp and jstl if you have please make it available .

    [email protected]

    thanking you always
     
    You will always be treated with dignity. Now, strip naked, get on the probulator and hold this tiny ad:
    We need your help - Coderanch server fundraiser
    https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
    reply
      Bookmark Topic Watch Topic
    • New Topic