• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HFSJ book:Bean Example Page 357

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo everyone,

this example is not working for me The rest of the examples in this chapter depend on this one so basically i am stock overhere

when i run the application i get no outupt and the parameter (user name) is passed in the URL. e.g. if i put "john" this is what i get in the address bar

http://localhost:8080/Beans3/result.jsp?userName=john

any clue?
***************************************
form.html

<html><body>
<form action="result.jsp">

name: <input type = "text" name="userName">
<input type="submit">

</form>
</body></html>
******************************************
result.jsp

<html><body>

<jsp:useBean id="person" class="com.example.web.Person" >

<jsp:setProperty name="person" property="name"
value="<%=request.getParameter("userName")%>" />
</jsp:useBean>
</body></html>
*******************************************************
java bean Person.java

package com.example.web;

public class Person {

private String name;

public void setName(String n)
{
name=n;
}

public String getName()
{
return name;
}
}//Person
**********************************
Test.java

package com.example.web;


import com.example.web.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;


public class Test extends HttpServlet{

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{


Person p = new Person();
p.setName("Evan");
request.setAttribute("person",p);

RequestDispatcher view= request.getRequestDispatcher("result.jsp");
view.forward(request,response);

}

}
**********************************
web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.4">

<servlet>
<servlet-name>BeansTest</servlet-name>
<servlet-class>com.example.web.Test</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>BeansTest</servlet-name>
<url-pattern>/Tester.do</url-pattern>
</servlet-mapping>

</web-app>

[ June 14, 2005: Message edited by: Jamed ]

[ June 14, 2005: Message edited by: Jamed ]

[ June 14, 2005: Message edited by: Jamed ]
[ June 14, 2005: Message edited by: Jamed ]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the code for your servlet?

Can you please type that also.

One more thing.Try doing this,

<jsp:setProperty name="person" property="name"
value="john" />

is it printing then?

Thanks,
Shrimon.
 
Jamed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shri,

i try what you have suggested but it's not working

i've posted my servlet Test.java at the the top

thanks in advance for you help
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Change form action to "Tester.do"

Also put <jsp:getProperty> in result.jsp to see whether it is set properly...

Radmika
 
Jamed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i've changed the action to Tester.do

it's still not working !

?
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
first thing, If your action in HTML form is result.jsp,so you don't need a servlet code. But, if you do want to use servlet code, then set the action to Tester.do.

second thing, in result.jsp, your setProperty tag would only execute if there is no person attribute in your page scope( as you have not specified the scope, it will default to page). If you specify the scope=request, the Container will search for the person attribute in the request scope.

third thing, you have not used the getProperty tag, that's why, your jsp is not displaying anything. use getProperty tag after your jsp:useBean tag ends.


hope this helps,
Regards

Sushma
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what you can do to get it working:

In result.jsp -
<html><body>

<jsp:useBean id="person" class="com.example.web.Person">
<jsp:setProperty name="person" property="name" value="<%=request.getParameter("userName")%>" />
</jsp:useBean>
<h1> The name of the person is: <jsp:getProperty name="person" property="name"/></h1>
</body>
</html>

In form.html -
<html><body>
<form action="Tester.do">

Name: <input type = "text" name="userName">
<input type="submit">

</form>
</body></html>

Even if you say <form action="result.jsp"> it will work without any problems but since you have a mapping in the web.xml for Tester.do it may be wise to use it.

Let me know if it works.

Thanks
Srinath
 
Jamed
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much srinath for you help

it did work with me with the solution provided by sushma

the problem was the i didn't use getProperty in my jsp and that's why it wasn't displaying anything when i tested it

but isn't this supposed to dipslay the name ?
"<%=request.getParameter("userName")%>"

thanks srinath and sushma for our hlep
[ June 17, 2005: Message edited by: Jamed ]
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic