You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Dahnke, Eric" <ED...@nextsource.com> on 2002/02/11 00:13:14 UTC

Using a JavaBean from within a servlet

Hello,

This is killing me. I've got a form that posts to a servlet. I simply want
to get the form variables into a bean's properties. 

I can find only one reference to what I'm trying to do here. it is a
formToBean() method from a FormUtils package, that some company sells.
Reading form variables into and out of javabean from a servlet has to be a
common activity. I can find heaps of info about using beans from JSP pages
(specifically about introspection), but I need to manipulate bean properties
from both Servlets and or JSPs. How do I do the introspection thing within a
servlet?




Code included below. 


JSP Post to a Servlet. -> Servlet instantiates a FormBean ->
FormBean.validate() is called. -> but the validate() is always false because
the bean property vals are empty. Do I have to explicitly read each
request.getParameter("FORM_VAR") and set that to a bean property?

// from the servlet
//////////////////////////////////////////////
FormBean fb = new FormBean();
fb.setProperty(*); // ***** this aint workin - here *****
if (fb.validate()) { 
	URL = "WELCOME";
} else {
	// go back
	URL = "INDEX";			
}


// from FormBean 
//////////////////////////////////////////////
private String UserName;
private String Password;

public boolean validate() {
	Debug.log (this, "validate","GETTING THIS FAR AT LEAST");

    	boolean allOk=true;
    	if (UserName.equals("")) {
      		errors.put("UserName","Please enter a username");
      		//UserName="";
      		allOk=false;
    	}
	if (Password.equals("") ) {
			errors.put("Password","Please enter a valid
password");
      		Password="";
      		allOk=false;
    	}
	return allOk;
}

public void setUserName(String uname) {
   	UserName = uname;
}
public void setPassword(String pword) {
   	Password =pword;
}

public String getUserName() {
	return UserName;
}
public String getPassword() {
	return Password;
}



SORRY IF THIS IS MORE OF A STRAIGHT JAVA QUESTION THAN A TOMCAT QUESTION. I
AM USING TOMCAT. JI JI. TIA.

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>


Re: Using a JavaBean from within a servlet

Posted by Charles Baker <ra...@yahoo.com>.
--- "Dahnke, Eric" <ED...@nextsource.com> wrote:
> Hello,
> 
> This is killing me. I've got a form that posts to a
> servlet. I simply want
> to get the form variables into a bean's properties. 
> 
> I can find only one reference to what I'm trying to
> do here. it is a
> formToBean() method from a FormUtils package, that
> some company sells.
> Reading form variables into and out of javabean from
> a servlet has to be a
> common activity. I can find heaps of info about
> using beans from JSP pages
> (specifically about introspection), but I need to
> manipulate bean properties
> from both Servlets and or JSPs. How do I do the
> introspection thing within a
> servlet?
> 
> 
> 
> 
> Code included below. 
> 
> 
> JSP Post to a Servlet. -> Servlet instantiates a
> FormBean ->
> FormBean.validate() is called. -> but the validate()
> is always false because
> the bean property vals are empty. Do I have to
> explicitly read each
> request.getParameter("FORM_VAR") and set that to a
> bean property?
> 
> // from the servlet
> //////////////////////////////////////////////
> FormBean fb = new FormBean();
> fb.setProperty(*); // ***** this aint workin - here
> *****
> if (fb.validate()) { 
> 	URL = "WELCOME";
> } else {
> 	// go back
> 	URL = "INDEX";			
> }
> 
> 
> // from FormBean 
> //////////////////////////////////////////////
> private String UserName;
> private String Password;
> 
> public boolean validate() {
> 	Debug.log (this, "validate","GETTING THIS FAR AT
> LEAST");
> 
>     	boolean allOk=true;
>     	if (UserName.equals("")) {
>       		errors.put("UserName","Please enter a
> username");
>       		//UserName="";
>       		allOk=false;
>     	}
> 	if (Password.equals("") ) {
> 			errors.put("Password","Please enter a valid
> password");
>       		Password="";
>       		allOk=false;
>     	}
> 	return allOk;
> }
> 
> public void setUserName(String uname) {
>    	UserName = uname;
> }
> public void setPassword(String pword) {
>    	Password =pword;
> }
> 
> public String getUserName() {
> 	return UserName;
> }
> public String getPassword() {
> 	return Password;
> }
> 
> 
<<SNIP>>

What I do is something like this:

// assuming we are creating a new session for login
HttpSession session = request.getSession(true);

FormBean formBean = new FormBean();

formBean.setUserName(
request.getParameter("userName"));

formBean.setPassword(request.getParameter("password"));

if ( formBean.validate()) {

    session.setAttribute("formBean", formBean);
    URL = "Welcome";

} else {

    URL = "Index";

}

=====
rascharles@yahoo.com
Hacking is a "Good Thing!"
See http://www.tuxedo.org/~esr/faqs/hacker-howto.html

__________________________________________________
Do You Yahoo!?
Send FREE Valentine eCards with Yahoo! Greetings!
http://greetings.yahoo.com

--
To unsubscribe:   <ma...@jakarta.apache.org>
For additional commands: <ma...@jakarta.apache.org>
Troubles with the list: <ma...@jakarta.apache.org>