1

I am trying to make AJAX call from html file to a servlet class to pass a variable but not getting any output. Please check the code below and suggest the changes that should be done.

This is my project structure

FrontPage.html

<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript">
    function func() {

            var build = document.getElementById('name').textContent;

            $.ajax({

                type : 'GET',
                url : 'http://localhost:8081/Sample/TestServlet',
                data : build,

                success : function(data) {
                    alert("Success");

                },
                error : function() {
                    alert("Error");
                }
            });
        }
    </script>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
</head>
<body>

    <a id="name" href="#" onClick="func();">Build1</a>

</body>
</html> 

This is TestServlet.java. This is trying to print variable which is passed from javascript function using Ajax call:

package com.infy;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestServlet
 */
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        response.setContentType("text/html");
        PrintWriter out=response.getWriter();

        out.println(request.getParameter("build"));

    }

}

Web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>Sample</display-name>
      <welcome-file-list>
        <welcome-file>FrontPage.html</welcome-file>   
      </welcome-file-list>
      <servlet>
        <description>Sample Servlet</description>
        <display-name>TestServlet</display-name>
        <servlet-name>TestServlet</servlet-name>
        <servlet-class>com.infy.TestServlet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>TestServlet</servlet-name>
        <url-pattern>/TestServlet</url-pattern>
      </servlet-mapping>
    </web-app>

I want to send the build variable to the servlet class and print the same from the servlet class but not getting any output.Please help me in fixing this.

2
  • how can you say that?Does it give exception on reading?Or does it has a empty value?.Note out.println(request.getParameter("build")); will send back the data to ajax call and not print it.if ajax call is not hitting servlet change url to /TestServlet Commented Mar 2, 2015 at 13:01
  • you are sending the request as GET method and you are not passing the variable in url.Try to print the build value in servlet first and check weather it is coming to servlet or not. Commented Mar 2, 2015 at 13:05

1 Answer 1

2

you are sending the request as GET method and you are not passing the variable in url.Try to print the build value in servlet first and check weather it is coming to servlet or not.

You should send the GET request as :-

url : "/TestServlet?build="+build+",
            type : "GET"

, And watch out the URL also. in servlet site create a json object.Like

JSONObject jsonObj = new JSONObject();

jsonObj.put("build", build);

Try it and let me know.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.