0

I have a java servlet which Reads data from a text file ,The o/p is then passed to a JS using ajax,The problem is I am successfully fetching the data from file but after passing it to ajax ,getting empty String

Java Script-

function main()
    {
      var result;

        $.ajax({
            url:'insertPos',
        //  data: {data : data},
            type:'get',             
             success:function(value)    
            {
                 result= value;
                 console.log(result);//getting empty string

            }

        });

    }

Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html");    
        PrintWriter out = response.getWriter();    

        BufferedReader br = new BufferedReader(new FileReader("D:/Workspace1/JAVA1/Sample/DATA.txt"));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null)
            {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
               String  value = sb.toString();
             System.out.println(value);


        } finally {
            br.close();
        }

    }

If I print in console console.log(result);//getting empty string

2
  • you are using ` System.out.println(value);` !! this will not write the send the value to ajax. Commented Apr 13, 2015 at 13:10
  • Here, you don't put anything to response writer. Commented Apr 13, 2015 at 13:11

2 Answers 2

2

Change your servlet with below code.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html");    
        PrintWriter out = response.getWriter();    

        BufferedReader br = new BufferedReader(new FileReader("D:/Workspace1/JAVA1/Sample/DATA.txt"));
        try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();

            while (line != null)
            {
                sb.append(line);
                sb.append(System.lineSeparator());
                line = br.readLine();
            }
               String  value = sb.toString();
               out.write(value);//you just need to use out that printwriter object.


        } finally {
            br.close();
        }

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

3 Comments

Getting this error-GET insertPos 500 Internal Server Error 17ms
D:\Workspace1\JAVA1\Sample\DATA.txt (The system cannot find the path specified)
ohh...you said that you can read file properly.....But not getting in ajax response. so you made this mistake. for file is it exist on the same path?
0

use

out.write(value);

instead of

System.out.println(value);

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.