users
[Top] [All Lists]

RE: [cinjug-users] newbie trying to learn m-v-c

To: <users@xxxxxxxxxx>
Subject: RE: [cinjug-users] newbie trying to learn m-v-c
From: "James Carman" <james@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 8 Sep 2004 18:00:35 -0400
Delivered-to: mailing list users@cinjug.org
Importance: Normal
In-reply-to: <20040908194021.3770.qmail@web52608.mail.yahoo.com>
Mailing-list: contact users-help@cinjug.org; run by ezmlm
Organization: Carman Consulting, Inc.
With JSTL (I assume you're using JSTL since you've defined the taglib in
your JSP page), you don't need ANY of that scriptlet code.  Why not use the
JSTL tags to print out your values?  You wouldn't have to worry about
casting anything to String.  You wouldn't have to import any classes.  JSTL
would take care of all of that for you.  
 

-----Original Message-----
From: sdgesa gaeharth [mailto:pollux1234567890@xxxxxxxxx] 
Sent: Wednesday, September 08, 2004 3:40 PM
To: users@xxxxxxxxxx
Subject: RE: [cinjug-users] newbie trying to learn m-v-c

Ah, I see. That works. However my original question is
to pass an object back to the jsp page. The
HttpServlet looks like this:

**********BEGIN SERVLET********
                MemberValidateForm formToValidate = new
MemberValidateForm();
                allOk =
formToValidate.validate(firstName,lastName,emailAddress,homePhone);
                if(allOk){
                        toPage = "/MemberAdd";
                }else{
                        req.setAttribute("formToValidate",formToValidate);
                }
                RequestDispatcher dispatcher;
                dispatcher = context.getRequestDispatcher(toPage);
                dispatcher.forward(req, res);

**********END SERVLET********

the jsp page looks like this:

**********BEGIN JSP********
<%@ taglib prefix="c"
uri="http://java.sun.com/jstl/core"%>
<%@ page import="java.util.*,
com.bdi.www.members.MemberValidateForm" %>
<%
MemberValidateForm doThis= (MemberValidateForm)
request.getAttribute("formToValidate");
out.println(doThis.getFirstName());
%>
**********END JSP********


MemberValidateForm looks like this:


**********BEGIN MemberValidateForm********

import java.util.*;

public class MemberValidateForm {

  private String firstName;
  private String lastName;
  private String emailAddress;
  private String homePhone;
  private Hashtable errors;

  public boolean validate(String firstName,String
lastName,String emailAddress,String homePhone) {
    boolean allOk=true;
    if (firstName.equals("")) {
      errors.put("firstName","Please enter your first
name");
      firstName="";
      allOk=false;
    }
    if (lastName.equals("")) {
      errors.put("lastName","Please enter your last
name");
      lastName="";
      allOk=false;
    }
    if (emailAddress.equals("") ||
(emailAddress.indexOf('@') == -1)) {
      errors.put("emailAddress","Please enter a valid
email address");
      emailAddress="";
      allOk=false;
    }
    if (homePhone.equals("")) {
      errors.put("homePhone","Please enter a valid
home phone number");
      homePhone="";
      allOk=false;
    }
    return allOk;
  }

  public String getErrorMsg(String s) {
    String errorMsg =(String)errors.get(s.trim());
    return (errorMsg == null) ? "":errorMsg;
  }

  public MemberValidateForm() {
    firstName="";
    lastName="";
    emailAddress="";
        homePhone="";
    errors = new Hashtable();
  }

  public String getFirstName() {
    return firstName;
  }

  public String getLastName() {
    return lastName;
  }

  public String getEmailAddress() {
    return emailAddress;
  }

  public String getHomePhone() {
    return homePhone;
  }

  public void setFirstName(String fname) {
    firstName =fname;
  }

  public void setLastName(String lname) {
    lastName =lname;
  }

  public void setEmailAddress(String eml) {
    emailAddress=eml;
  }

  public void setHomePhone(String hp) {
    homePhone=hp;
  }

  public void setErrors(String key, String msg) {
    errors.put(key,msg);
  }

}
**********END MemberValidateForm********


thanks

--- "Jeykumar, Nattamai S. (LNG-DAY)"
<nattamai.jeykumar@xxxxxxxxxxxxxx> wrote:

> Try the following...
> 
> 
> Typecast the object returned by
> request.getAttribute("toPage");
> 
> String tttt = (String)
> request.getAttribute("toPage");
> 
> 
> 
> Thanks,
> Jey Kumar
> 
> 
> 
> -----Original Message-----
> From: sdgesa gaeharth
> [mailto:pollux1234567890@xxxxxxxxx]
> Sent: Wednesday, September 08, 2004 3:10 PM
> To: Ted Tollefson
> Cc: users@xxxxxxxxxx
> Subject: RE: [cinjug-users] newbie trying to learn
> m-v-c
> 
> 
> For some reason I still get this error. I know its
> something i am doing wrong that is really stupid!!!
> I
> just cant figure
> 
> ******************
> incompatible types
> found   : java.lang.Object
> required: java.lang.String
> String tttt = request.getAttribute("toPage");
> ******************
> 
> thanks again
> 
> 
> 
> --- Ted Tollefson <ttollefson@xxxxxxxxxxxxxxxxxxxxx>
> wrote:
> 
> > my bad.  It should be
> >  
> > request.getAttribute("...
> >  
> > not request.getParameter
> > 
> > ________________________________
> > 
> > From: sdgesa gaeharth
> > [mailto:pollux1234567890@xxxxxxxxx]
> > Sent: Wed 9/8/2004 11:49 AM
> > To: users@xxxxxxxxxx
> > Subject: RE: [cinjug-users] newbie trying to learn
> > m-v-c
> > 
> > 
> > 
> > Thanks for the reply.I tried your code and i got
> an
> > error saying "cannot resolve symbol(get), so i
> > changd
> > it to:request.getParameter. Now I get this error
> > message when trying your code:
> > 
> > inconvertible types
> > found   : java.lang.String
> > required: com.bdi.www.members.MemberValidateForm
> >  MemberValidateForm formToValidate =
> >
>
(MemberValidateForm)request.getParameter("formToValidate");
> > 
> > thanks again
> > 
> > 
> > 
> > 
> > --- Ted Tollefson
> <ttollefson@xxxxxxxxxxxxxxxxxxxxx>
> > wrote:
> > 
> > > In your JSP, you can call this:
> > > 
> > > <%
> > > 
> > >  MemberValidateForm formToValidate =
> > > (MemberValidateForm)
> > request.get("formToValidate");
> > >
> > >
> >
>
out.println(formToValidate.getErrorMsg("firstName"));
> > > 
> > > %>
> > >
> > >
> > > ________________________________
> > >
> > > From: sdgesa gaeharth
> > > [mailto:pollux1234567890@xxxxxxxxx]
> > > Sent: Tue 9/7/2004 7:43 PM
> > > To: users@xxxxxxxxxx
> > > Subject: [cinjug-users] newbie trying to learn
> > m-v-c
> > >
> > >
> > >
> > > hello,
> > >
> > > as you can see i have a simple app here. How can
> i
> > > get
> > > the error msgs and form values to show up when
> the
> > > servlet detects a form validation error?
> > >
> > > I can do a System.out.println in
> > MemberSerlvet.java
> > > (below) and see the proper error message. But i
> > cant
> > > get it passed to the jsp page properly.
> > >
> > > please help!
> > >
> > >
> > > MemberValidateForm formToValidate = new
> > > MemberValidateForm();
> > >                 allOk =
> > >
> >
>
formToValidate.validate(firstName,lastName,emailAddress,homePhone);
> > >
> >
>
System.out.println(formToValidate.getErrorMsg("firstName");
> > >                 if(allOk){
> > >                         toPage = "/MemberAdd";
> > >                 }
> > >               
> > >
> req.setAttribute("formToValidate",formToValidate);
> > >
> > >
> > >               
> > > __________________________________
> > > Do you Yahoo!?
> > > Yahoo! Mail - 50x more storage than other
> > providers!
> > > http://promotions.yahoo.com/new_mail
> > >
> > >
> > 
> > > ATTACHMENT part 2 application/ms-tnef
> > name=winmail.dat
> > > ---------
> > > You may unsubscribe from this mailing list
> > > by sending a blank email addressed to:
> > > users-unsubscribe@xxxxxxxxxx
> > >
> > > --
> > > Find additional help by sending a blank email
> > > addressed to:
> > > users-help@xxxxxxxxxx
> > 
> > 
> > 
> >                
> > _______________________________
> > Do you Yahoo!?
> > Win 1 of 4,000 free domain names from Yahoo! Enter
> > now.
> > http://promotions.yahoo.com/goldrush
> > 
> > ---------
> > You may unsubscribe from this mailing list
> > by sending a blank email addressed to:
> > users-unsubscribe@xxxxxxxxxx
> > 
> > --
> > Find additional help by sending a blank email
> > addressed to:
> > users-help@xxxxxxxxxx
> > 
> > 
> > 
> > 
> 
> 
> 
>               
> __________________________________
> Do you Yahoo!?
> New and Improved Yahoo! Mail - Send 10MB messages!
> http://promotions.yahoo.com/new_mail 
> 
> ---------
> You may unsubscribe from this mailing list
> by sending a blank email addressed to:
> 
=== message truncated ===


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------
You may unsubscribe from this mailing list
by sending a blank email addressed to:
users-unsubscribe@xxxxxxxxxx

--
Find additional help by sending a blank email
addressed to:
users-help@xxxxxxxxxx



<Prev in Thread] Current Thread [Next in Thread>