-1

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.

ZaredH
  • 491
  • 1
  • 7
  • 23
  • [This](http://stackoverflow.com/questions/10214723/jquery-ajax-post-data) might help with how to format the data for the ajax POST. – Andrew S Sep 06 '16 at 21:27
  • 1
    Couldn't quite get from the description- what exactly is your problem. I notice several issues with your code though ... I'll name two **1.** you are using `struts2` however, `ajax` call is handled by a `servlet` not a `struts2` `action` **2.** in `ajax` call you are sending variable as `uname` however in `servlet` you are expecting to receive `user.username` – PKey Sep 07 '16 at 05:37
  • 1
    Frankenstein!! :D Create an action instead of a servlet. Usually, a class extending ActionSupport with an execute() method. – Andrea Ligios Sep 07 '16 at 09:01
  • @Plirkee changing the variable did the trick. The code executes as expected. However after check.java executes, no message is displayed on the GUI. My status div is stuck with the "Checking availability..." message from earlier. – ZaredH Sep 07 '16 at 13:03

1 Answers1

-1

I found my solution. I changed my java class to extend ActionSupport based on Andrea's suggestion. I changed my struts action result to be of type stream.

<action name="usernameCheck" class="java.com.my.package.check" method="doGet">
    <result type="stream">
        <param name="contentType">text/html</param>
        <param name="inputName">stream</param>
    </result>
</action>

I added the following to my java class

private InputStream stream;

public InputStream getStream() {
        return stream;
    }

And changed my JavaScript success function to the following:

success : function(msg) {
    $(".status").html(msg);
}

Thank you all for all the input.

ZaredH
  • 491
  • 1
  • 7
  • 23