I'm building a web app using Struts2 as a framework. As the title suggest, I'm attempting to check availability of a username using an AJAX call (open to other suggestions if there is something easier that accomplishes the same result.)
Right now I'm having two issues:
- Request is null in my doPost function.
- Even if I set uname to a string, nothing happens.
Here is what I have for code.
Login.jsp
<s:form action="login.action" method="post" data-parsley-validate="">
            <s:textfield name="user.username" key="Username" id="username" onchange="checkUsername()"/><span class="status"></span>
            <s:submit method="doTheThing" align="center" onclick="hide()"/>
        </s:form>
Javascript
function checkUsername(){
    var uname = $(username).val();
    if (uname.length > 3) {
        $(".status").html("Checking availability...");
        $.ajax({
            type : "POST",
            url : "usernameCheck.action",
            data : "uname=" + uname,
            success : function(msg) {
                $(".status").ajaxComplete(
                        function(event, request, settings) {
                            $(".status").html(msg);
                        });
            }
        });
    }
    else {
        $(".status").html("username should be at least 3 characters");
    }
}
check.java
public class check extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        ActionSupport connectionSupport = new ActionSupport();
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Connection connection = null;
            String URL = "172.ExampleIP";
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(URL, "user","pass");
            String uname = request.getParameter("user.username");
            PreparedStatement ps = connection.prepareStatement("select username from users where username=?");
            ps.setString(1, uname);
            ResultSet rs = ps.executeQuery();
            if (!rs.next()) {
                out.println("<b>" + uname + "</b> is avaliable");
            } else {
                out.println("<font color=red><b>" + uname + "</b> is already in use</font>");
            }
            out.println();
        } catch (Exception ex) {
            out.println("Error ->" + ex.getMessage());
        } finally {
            out.close();
        }
    }
    public void doGet()
            throws ServletException, IOException {      
        doPost(ServletActionContext.getRequest(), ServletActionContext.getResponse());
    }
}
Struts.xml
<action name="usernameCheck" class="java.com.my.package.check" method="doGet"></action>
I'm not sure if this is important information, but the form is on a JQuery modal box.
