• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Jsps and Servlets

 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you make a call to "/PrintBean.jsp" that page opens up. Just check what is the output.

Regards
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
  •  
    Himanshu Kansal
    Ranch Hand
    Posts: 257
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 664
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 664
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 277
    Eclipse IDE Java PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Christophe Verré wrote:

    You left out the ".jsp" extension.
     
    Christophe Verré
    Sheriff
    Posts: 14691
    16
    Eclipse IDE Ubuntu VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You left out the ".jsp" extension.

    Oops sorry. Actually, the jsp-file tag should not be used at all.
     
    Nabila Mohammad
    Ranch Hand
    Posts: 664
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 277
    Eclipse IDE Java PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 664
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Michael!
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic