FileDocCategorySizeDatePackage
AjaxSignupServlet.javaAPI DocExample2874Wed Apr 05 20:05:42 BST 2006com.oreilly.ajax.servlet

AjaxSignupServlet

public class AjaxSignupServlet extends HttpServlet

Fields Summary
private static final long
serialVersionUID
Constructors Summary
Methods Summary
private booleanaddUser(javax.servlet.http.HttpServletRequest req)

		Connection con = DatabaseConnector.getConnection();
        //Hard coded values. Wouldn't it be better for the form values
        // to just match the database?
        String username = req.getParameter("ajax_username");
        String firstname = req.getParameter("firstname");
        String lastname = req.getParameter("lastname");
        String email = req.getParameter("email");
        String password = req.getParameter("password");
        String address = req.getParameter("address");
        String city = req.getParameter("city");
        String state = req.getParameter("state");
        String zipcode = req.getParameter("zipcode");
		try {
			Statement add = con.createStatement();
			String sqlstatement = "INSERT USERS (USERNAME,PASSWORD,FIRST_NAME,LAST_NAME,EMAIL,ADDRESS,CITY,STATE,ZIPCODE) " +
					" VALUES('"+username+"','"+password+"','"+firstname+"','"+lastname+"','"+email+"','"+address+"','"+
					city+"','"+state+"','"+zipcode+"');";
			add.execute(sqlstatement);
			
		} catch (SQLException e) {
            // TODO: use log4j or other loggin system instead
            System.out.println("exception in adding user '"+username+"','"+password+"','"+firstname+"','"+lastname+"','"+email+"','"+address+"','"+
                    city+"','"+state+"','"+zipcode+" message"+e.getMessage());
			return false;
		} finally{
            if(con != null)
            {
                try{
                    con.close();
                }catch(SQLException e){}
                
            }
        }
		return true;

	
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)


	     
			   
        ServletContext sc = getServletContext();
        RequestDispatcher rd = null;

        if(addUser(req))
        {
            rd = sc.getRequestDispatcher("/confirmation.html");
            
            rd.forward(req, res); 
        }
		else {
            rd = sc.getRequestDispatcher("/failure.html");
            rd.forward(req, res);
		}