wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Jsps and Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Jsps and Servlets" Watch "Jsps and Servlets" New topic
Author

Jsps and Servlets

Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I am not sure how the jsp and servlet works together. And how they are linked.

My code is working indepenedent of the servlet which I had added later . And i cant link it to the jsp after using web.xml
Why isn't my servlet setting the value of dog and person.
I have been playing with different code on the same application n have sort of mixed everything together.

Any way here is all my code.

PrintBean.jsp



MySerlvet



My web.xml


There is a good possibility i have messed things up here.

Can some explain how things are working and what is executed first .
Thanks!


The future belongs to those who believe in the beauty of their dreams.Dream BIG!
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
When you make a call to "/PrintBean.jsp" that page opens up. Just check what is the output.

Regards


Experience and talent are independent of age
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
My out put is :


Person is null

Dog is foo.Dog@1e9c82e

in EL Person.name is
in EL Person.dog is foo.Dog@1e9c82e

in EL Person.dog.name is



How come I am not getting the values I have set in my Servlet?


Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Well, you ARE getting the values from the servlet. The hex code you got belongs to the dog object set in the servlet.

You got person as null because at line 8, you did not set a value for the bean. Either a "param" or a "value" attribute should be present. For your case I think "property=*" should do.

Regards
Nabila Mohammad
Ranch Hand

Joined: Nov 05, 2007
Posts: 661
I am sorry ,

My out put is actually :
----------------------------
Person is null

Dog is null

in EL Person.name is
in EL Person.dog is

in EL Person.dog.name is
-----------------------------
I was using the wrong Person.java file

Anyway,I dont think I am getting the values fromt he servlet.
If i did , I would be getting the value of "Evan" for Person.name and "Spike' for Person.Dog.name.
I guess property="*" is used when you want to display the values you have input.That means the name of the request parameters should match the property of the bean.
It doesn't work here.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16


This is creating a Person into the page scope. The Person you have set from your servlet is in request scope. When you use ${person.name}, the page scoped attribute will be found first, thus all the null and blank values.
You have the choice between :
  • Removing the useBean tag, so that ${person} will return the Person in request scope
  • Explicitly get the person from the request scope, using ${requestScope.person}
  • Set the scope of the useBean tag to "request", so that the Person in request scope is correctly fetched


  • [My Blog]
    All roads lead to JavaRanch
    Himanshu Kansal
    Ranch Hand

    Joined: Jul 05, 2009
    Posts: 257
    I guess property="*" is used when you want to display the values you have input.That means the name of the request parameters should match the property of the bean.


    They are matching.
    Nabila Mohammad
    Ranch Hand

    Joined: Nov 05, 2007
    Posts: 661
    I wasn't sure what you meant by
    Removing the useBean tag, so that ${person} will return the Person in request scope


    But I had tried
    -Explicitly get the person from the request scope, using ${requestScope.person}
    -Set the scope of the useBean tag to "request", so that the Person in request scope is correctly fetched

    And still got the same results

    Is it something to do with my File structure.
    I have my MyServlet.class ,Person.class, Dog.class files in WEB-INF/classes/foo/ folder
    Web.xml in WEB-INF folder
    And PrintBean.jsp in Testing(name of the application) folder .
    Christophe Verré
    Sheriff

    Joined: Nov 24, 2005
    Posts: 14687
        
      16

    Can you post the url you are using to access your page ? The fact that you have no doGet in your servlet is puzzling me.
    Nabila Mohammad
    Ranch Hand

    Joined: Nov 05, 2007
    Posts: 661
    Even if I change the doPost() to doGet() I still get the same resuts.

    My url : http://localhost:8080/Testing/PrintBean.jsp
    Christophe Verré
    Sheriff

    Joined: Nov 24, 2005
    Posts: 14687
        
      16

    I should have checked your web.xml first. You have mapped "/PrintBean.jsp" to your servlet, although there's a file called PrintBean.jsp at the root. You're not accessing your servlet at all. You're accessing your JSP file directly. If you were accessing your servlet, you'd get an error because you are not overriding doGet.

    Try this :


    Now, you will need to change doPost to doGet.

    And use the following url :
    http://localhost:8080/Testing/PrintBean
    Michael Angstadt
    Ranch Hand

    Joined: Jun 17, 2009
    Posts: 273

    Christophe Verré wrote:

    You left out the ".jsp" extension.


    SCJP 6 || SCWCD 5
    Christophe Verré
    Sheriff

    Joined: Nov 24, 2005
    Posts: 14687
        
      16

    You left out the ".jsp" extension.

    Oops sorry. Actually, the jsp-file tag should not be used at all.
    Nabila Mohammad
    Ranch Hand

    Joined: Nov 05, 2007
    Posts: 661
    Thanks alot Christope!


    I got the right answer when the mapping was :




    and NOT



    I Thought when I want to displayresult the jsp file I would be writing ><jsp-file>
    So how exactly is this working...
    Michael Angstadt
    Ranch Hand

    Joined: Jun 17, 2009
    Posts: 273

    Nabila Mohammad wrote:I Thought when I want to displayresult the jsp file I would be writing ><jsp-file>
    So how exactly is this working...

    No, if you want a servlet to use a JSP (i.e. you want your servlet code to run first, then forward control to the JSP), you've got to do something like this:
    Nabila Mohammad
    Ranch Hand

    Joined: Nov 05, 2007
    Posts: 661
    Thanks Michael!
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Jsps and Servlets