FileDocCategorySizeDatePackage
AjaxUsernameServlet.javaAPI DocExample2261Wed Apr 05 20:05:42 BST 2006com.oreilly.ajax.servlet

AjaxUsernameServlet

public class AjaxUsernameServlet extends HttpServlet

Fields Summary
private static final long
serialVersionUID
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)


         
              
    
        String username = req.getParameter("username");
        if (username != null)
        {
            if (existsUsername(username))
            {
                res.setContentType("text/xml");
                res.setHeader("Cache-Control", "no-cache");
                res.getWriter().write("true");
            }
        } else
        {
            // If key comes back as a null return a message.
            res.setContentType("text/xml");
            res.setHeader("Cache-Control", "no-cache");
            res.getWriter().write("Username null");
        }
    
private booleanexistsUsername(java.lang.String username)

        ResultSet result = null;
        Connection con = null;
        try
        {
            con = DatabaseConnector.getConnection();
            Statement select = con.createStatement();
            result = select
                    .executeQuery("SELECT USERNAME from USERS where USERNAME = '"
                            + username + "';");

            if (result == null || result.next())
            {
                return true;
            }
        } catch (SQLException e)
        {
            // use log4j or handle this how you want.
        } finally
        {
            if (con != null)
            {
                try
                {
                    con.close();
                } catch (SQLException e)
                {
                }

            }
        }
        return false;