Re: Request form variable in a class
You no-argument constructor for your First class attempts to set lastname to
a value you take from an instance variable (the request object) which is
never explicitly initialized. Any non-primitive object type instance
variables that don't have value assignments get instantiated to null. That
is where your NullPointerException is coming from. You are going to get a
null pointer every time you try to construct an instance of your First class
using the no-argument constructor you have given it as it is currently.
However, you need not give the First class an internal reference to the HTTP
request object at all really. There is a little process called
introspection that you can use to your advantage to populate the instance
fields of your bean from values in the jsp's request object. For this to
work all you have to do is create a javabean class that follows javabeans
specifications for naming of instance variables and their getters/setters.
Then in your jsp you can do something like this:
<jsp:useBean id="firstBean" scope="request" class="your.package.name.First"
/>
<jsp:setProperty name="firstBean" property="*" />
This will cause the jsp engine to first look for an existing instance of
your bean class named "firstBean" in the specified scope (request in this
case) and assign it to a reference called "firstBean." If no existing
instance is found, one will be instantiated using the bean's public no-arg
constructor and assigned to "firstBean." The second tag will then cause all
the setter methods on your "firstBean" class with names that match your
request parameters (according to javabeans specifications) to be called.
You never need to do any explicit setting in your bean constructor nor pass
any reference to the http request object to your bean. The jsp engine will
invoke each setter method on your bean for which it finds a request
parameter with a name that matches a bean setter method. Keep in mind http
request parameters are always Strings, so all your bean setter methods
should take String arguments for this to work.
See this as a reference:
http://java.sun.com/products/jsp/syntax/2.0/syntaxref20.html
That is the Sun JSP 2.0 specification jsp tag library syntax reference
sheet.
Look at the references for jsp:useBean and jsp:setProperty
<francan00@yahoo.com> wrote in message
news:3d704367-a972-4f13-a11d-751d0b2db46e@e39g2000hsf.googlegroups.com...
In my Tomcat 4.1.27 container (with no struts), I am trying to pass a
form value to a JavaBean.
Here is my attempt and need advise on the best way to fetch the
lastname value in my below Java class because it does compile but
gives me a NullPointerException in the JSP when I call this JavaBean:
public class First
{
private HttpServletRequest request;
private String lastname;
public First()
{
lastname= (String) request.getParameter("lastname");
}
/* Getter and Setter Methods */
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public FetchFormValue()
{
if(lastname.equals("Smith")
{
//do stuff for Smith
}
else
{
//do other stuff
}
}
..
}
Please advise.