tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Charles Baker <raschar...@yahoo.com>
Subject Re: Using a JavaBean from within a servlet
Date Mon, 11 Feb 2002 00:27:48 GMT

--- "Dahnke, Eric" <EDahnke@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:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>


Mime
View raw message