0
I have created a dynamic web project in eclipse.

It contains a Servlet ResidentApi.java and two java classes:GeoLocationDemo.java and Geolocation.java. I am calling GeoLocationDemo.java from my servlet and getting result in a ResultSet.But i am not getting any value in ResultSet.

When i ran same GeoLocationDemo.java separatly i am getting right results.I don't know servlet is able to call my java class or not but if it is then why i am not getting results.

I am having hard time debugging it.What i am doing is running .war file of this project every time on tomcat server and checking results there.Please suggest a good method to test it on eclipse.

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


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

    ResultSet rs = null;
    try{    
        GeoLocationDemo geo = new GeoLocationDemo();   //Here i created a new object
         rs = geo.getResults();                       //here i called a method of GeoLocation
    }catch(Exception e){
        // System.out.println(e);
     out.write("<head><b You suck</b></head>");
    }

    out.write("<head><b>Congratulation! connected</b></head>");    //i am getting this output
     try{     
         while(rs.next()){
         String s = rs.getString("Details");
         out.write("<head><b> "+s+ " </b></head>");           //not able to get this output
     }

        }catch(Exception e){
                // System.out.println(e);
                 out.write("<head><b>You Built </b></head>");
             }

         out.close();


}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ajay Singh
  • 31
  • 1
  • 1
  • 9

2 Answers2

0

Don't put everything in a <head> tag! Open a body somewhere. Don't silently swallow any Exception you might be getting. Do remember to close() the ResultSet. Also, you should probably be returning all of your data in a List with a POJO.

out.write("<body>");
out.write("<b>Congratulation! connected</b><br/>");
try {
    while (rs.next()) {
        String s = rs.getString("Details");
        out.write("<b> "+s+ "</b><br/>");
    }
} catch (Exception e){
    // System.out.println(e);
    out.write("<b>" + e.getMessage() + "</b>");
    e.printStackTrace(out);
} finally {
    try { 
        out.close(); 
    } catch (Exception ignored) {
    }
    try {
        rs.close();
    } catch (Exception ignored) {
    }
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0
  1. configure remote debugging between your IDE and Tomcat server e.g. take a look on Remote debugging Tomcat with Eclipse
  2. localize the problem - is the problem in GeoLocationDemo or in ResultSet or in output processing

p.s. do not close resources you never open - out.close(); - it is managed by servlet container

Community
  • 1
  • 1
ursa
  • 4,404
  • 1
  • 24
  • 38