Request form variable in a class

F

francan00

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.
 
L

Lew

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:

Of course, you've conveniently neglected to show us how you invoke it in the
JSP, which, of course, is actually the wrong place to handle request
parameters in the first place.

Even, or perhaps especially, without Struts (not "struts" - spelling counts),
you should submit all forms to a controller servlet that cracks the parameters
and passes them to the business logic, then embeds the logic bean or a result
bean in the request attributes, then forwards to the appropriate next JSP,
which will display it. (Forwarding accomplished by the RequestDispatcher
returned from request.getRequestDispatcher( pathToNextJsp )).
public class First
{
private HttpServletRequest request;
private String lastname;

public First()
{
lastname= (String) request.getParameter("lastname");

And just where is your new First supposed to get the 'request' variable from?

Read up on the "Model 2" Model-View-Controller (MVC) pattern on the Sun web
site or via Google. GIYF.
}

/* Getter and Setter Methods */

But none for 'request'. You should retrieve the "lastname" parameter from the
request in your controller servlet, then pass lastname to whatever logic needs
it (or embed it directly as a request attribute using request.setAttribute()).
public FetchFormValue()
{
if(lastname.equals("Smith")
{
//do stuff for Smith
}
else
{
//do other stuff
}
}
...
}

This logic would be called from a logic class instance within the controller
servlet.
 
J

Joe Blow

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top