1

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

2 Answers 2

2

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); 
Sign up to request clarification or add additional context in comments.

7 Comments

can i use any other method in the type insted of get/post and can i write return "json"; in method. i am using only servlet for entire application
see in servlet doPost or doGet will be called as per the form method(i.e get or post).
thanks it is working fine but i have doubt if i use so many data base quiries in one servlet how to differentiate only that method . generally we write public String method(){ return "jsonstring";} but here not possible to write return statement but unnecessarily calling entire servlet
@sarath sorry could not understand
@sarath you can write out.println(method());
|
0
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+";");

6 Comments

it is returning json string i converted JSON.parse() now i want display a table format first record must be input type radio
display first record? loop json datas to get first record...is it?(是这个意思吗?)
{'location':hyderbad,'track':12345,'myDate ':15-dec-2013}{'location':sydny,'track':2322,'myDate ':21-jan-2014} retuning data in this format it is showing error parse to json
{'location':'hyderbad','track':'12345','myDate ':'15-dec-2013'}{'location':'sydny','track':'2322','myDate ':'21-jan-2014'} it is json data style
it needs "," from one object another object
|

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.