Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes HFSJ book:Bean Example Page 357 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 "HFSJ book:Bean Example Page 357" Watch "HFSJ book:Bean Example Page 357" New topic
Author

HFSJ book:Bean Example Page 357

Jamed
Ranch Hand

Joined: Apr 17, 2005
Posts: 35
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 ]
shri mon
Ranch Hand

Joined: Jan 11, 2005
Posts: 63
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

Joined: Apr 17, 2005
Posts: 35
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
Radmika Arunachalam
Ranch Hand

Joined: Mar 29, 2004
Posts: 45
Change form action to "Tester.do"

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

Radmika
Jamed
Ranch Hand

Joined: Apr 17, 2005
Posts: 35
i've changed the action to Tester.do

it's still not working !

?
Sushma Sharma
Ranch Hand

Joined: Jun 02, 2005
Posts: 139
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
srinath anand
Ranch Hand

Joined: Nov 20, 2001
Posts: 30
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


SCJP2,SCWCD 1.4
Jamed
Ranch Hand

Joined: Apr 17, 2005
Posts: 35
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HFSJ book:Bean Example Page 357
 
Similar Threads
JSP - cannot resolve symbol
EL question
HFSJ EL e.g. page 371
Unable to getParameter in jsp
HFSJ book:Bean Example Page 345