0

I am just sending simple parameters through jsp and receiving this parameters through javascript in servlet page and trying to call doGet method of servlet through $.ajax() method but my code is not working. I know I am wrong somewhere please correct me. I am using jsp page for input and ajax for sending request and servlet for request handling and I am not using any jar files for this code.

index.jsp:

<script src="js/register.js" type="text/javascript"></script>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>jQuery, Ajax and Servlet/JSP integration example</title>

    <script src="http://code.jquery.com/jquery-1.10.2.js"
        type="text/javascript"></script>
    <script src="js/register.js" type="text/javascript"></script>
    </head>
    <body>

        <form>
            Enter Your Name: <input type="text" id="userName" />
                    <input type="submit" id="editCategory" class="add-new-user" name="btnid" value = "Submit" onclick="send();">

        </form>
        <br>
        <br>

        <strong>Ajax Response</strong>:
        <div id="ajaxGetUserServletResponse"></div>
    </body>
    </html>

register.js:

function send() {
    $.ajax({
        method: "GET",
        url: 'RegisterServlet',

        data: {
            userName: $('#userName').val()
        },
        success: function (responseText) {
            $('#ajaxGetUserServletResponse').text(responseText);
        }
    );
}

RegisterServlet.java:

public class RegisterServlet extends HttpServlet  {

    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse   response) throws ServletException, IOException {

        String userName = request.getParameter("userName").trim();
        if(userName == null || "".equals(userName)){
            userName = "Guest";
        }

        String greetings = "Hello " + userName;

        response.setContentType("text/plain");
        response.getWriter().write(greetings);
    }
}
Jack Flamp
  • 1,223
  • 1
  • 16
  • 32
Vijay
  • 197
  • 10

1 Answers1

0

What is not working?

You are calling onclick in a input type submit which is a little wrong I think. Use <button> instead. And you don't need to put it inside a <form> tag. If you want to stay with input type submit then you need to return false in your javascript function.

The thing is that you want to send an AJAX call and not submit a form. So use <button> tag instead.

 Enter Your Name: <input type="text" id="userName" />
 <button onclick="send();">

and:

function send() {
    $.ajax({
        method: "GET",
        url: 'RegisterServlet',
        data: {
            userName: $('#userName').val();
        },
        success: function (responseText) {
            $('#ajaxGetUserServletResponse').text(responseText);
        }
    });
    return false;
}
Jack Flamp
  • 1,223
  • 1
  • 16
  • 32