jquery ajax calling servlet method and return json data

I am new to Ajax programming I am using Jquery ajax my requirement to to fetch the data into server and display as table format.In that location must display radio button type. how to call servlet specific method how to return data.i am ready to use json.can any one help me how to call a method how to return a data.and suggestions require to solve the problem

thanks in advance.

$('#ajaxbutton').on('click',function(){
                $.ajax({
                    type:"post",
                    url:"Db2",
                    data:{"labid",100},
                    sucess:function(){
                        alert("sucess");
                    }

                });
            });

in servlet

public class Db2 extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws MalformedURLException, IOException {
        doProcess(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws MalformedURLException, IOException {
        doProcess(request, response);
    }

    public void doProcess(HttpServletRequest request,
            HttpServletResponse response) throws MalformedURLException,
            IOException {
        Connection con;
        PreparedStatement ps, ps1;
        PrintWriter out = response.getWriter();

        try {

            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:oracle1", "system",
                    "sarath");
            ps = con.prepareStatement("select trackid,location,to_char(mydate,'dd-mon-yyyy') from information where labid=100");
            ResultSet rs = ps.executeQuery();

            while (rs.next()) {
                String location = rs.getString(2);
                String track = rs.getString(1);
                String myDate = rs.getString(3);
            }

        } catch (Exception e) {
            out.println(e);
        }

    }
}// end of ServletB

Upvotes: 1

Views: 14772

Answers (2)

heqing
heqing

Reputation: 24

PrintWriter out = response.getWriter();
StringBuffer res = new StringBuffer();
while (rs.next()) {
    String location = rs.getString(2);
    String track = rs.getString(1);
    String myDate = rs.getString(3);
    res.append("{");
    res.append("'location':");
    res.append(location);
    res.append(",");
    res.append("'track':");
    res.append(track);
    res.append(",");
    res.append("'myDate ':");
    res.append("myDate ");
    res.append("}");
}
out.println(res.toString());

must be json-string and the ajax can use eval("data = "+r_data+";");

Upvotes: 0

SpringLearner
SpringLearner

Reputation: 13844

how to call servlet specific method In your ajax you are writing type=post so in the servlet doPost() will be called.if it would have been type=get then servlet doGet() method will be called.So you should write the database retrieve part in the specific method.

Now suppose you want location,track and mydate then try this way In the servlet

PrintWriter out.response.getWriter();
out.println(location);
out.println(track);
out.println(mydate);

In the ajax success part do this way

success: function(data, textStatus, jqXHR){
                       alert(data); 

Upvotes: 2

Related Questions