tags:

views:

44

answers:

2
sos.print("{success:true}");
sos.close();
  1. What is sos?
  2. Whay do we close sos?
  3. Where does it print, in the console or somewhere.

Is it something like a return statement

return "{success:true}";

Can i also pass my ajax response like this

Update... I have updated the entire code here.

protected void process(HttpServletRequest request, HttpServletResponse response) {

          try {
            ServletOutputStream  sos = response.getOutputStream();
            response.setHeader("Cache-Control","no-store"); 
            response.setHeader("Pragma","no-cache");
            response.setContentType("text/plain");
            String name = request.getParameter("name");
            String age = request.getParameter("age");
            String city = request.getParameter("city");
            String phone = request.getParameter("phone");

            System.out.println("Name: " + name);
            System.out.println("Age: " + age);
            System.out.println("City: " + city);
            System.out.println("Phone: " + phone);

            String query ="INSERT INTO CRUD_DATA VALUES('"+name+"',"+age+",'"+city+"',"+phone+")";
            System.out.println("Query:" + query);

            OracleDataSource ods = new OracleDataSource();
            ods.setUser("abdel");
            ods.setPassword("password");
            ods.setURL("jdbc:oracle:thin:@//127.0.0.1/XE");

            Connection conn = ods.getConnection();
            Statement statement = conn.createStatement();

            statement.executeUpdate(query);
            conn.commit();
            conn.close();           

            sos.print("{success:true}");
            sos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
+2  A: 

What is sos?
You should check the code you've taken it from and see how sos is defined. In Java, object behaviour is defined by its type and not name.

Whay do we close sos?
shit, I've been reading it "why do we call it 'sos'" :)
Probably, its class is S***OutputStream

Where does it print, in the console or somewhere.
Again, my guess is that it prints data into a buffer of ServletResponse object.

edit
So, this is the important line to understand what sos is

ServletOutputStream  sos = response.getOutputStream();

You can check documentation for ServletOutputStream object and getOutputStream method.

Can i also pass my ajax response like this
Yes, you can pass response from your Java servlet to client's browser like this.

Nikita Rybak
updated the entire code
John
+1  A: 
  1. It's a ServletOutputStream
  2. To release all system resources associated with this stream.
  3. It is sent back to client in response
Tadeusz Kopec