1 Replies - 718 Views - Last Post: 31 March 2011 - 07:08 AM

#1 shaboury   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 52
  • Joined: 30-May 09

Servlet output changed

Posted 22 March 2011 - 09:46 AM

Weird thing happened to me
that s a function in a class where i can manipulate and get data from database and i tested it in a main and i got the right data
 
public String getCurrentLoc()
	{
		
		String location= "Undefined";
		try{
			System.out.println("Done");
			Statement stmt=con.createStatement();
			ResultSet rs=stmt.executeQuery("Select Location from  sheet1  Where Status= 'Current'");
			if(rs.next())
				{
				
				
					location=rs.getString(1);
					
					
				}
			
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
	return location;	
	
	}


and i created an object of this class in a servlet
package BusTrackingSystem;
import java.io.*;
import java.util.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.swing.text.html.HTMLdocument.Iterator;

public class getLoc extends HttpServlet{
	

  private ServletConfig config;
  DButils c= new DButils();
  public void init(ServletConfig config)
    throws ServletException{
     this.config=config;
     }
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException,IOException {
    
    PrintWriter out = response.getWriter();
    
    ArrayList<String> dataList = new ArrayList<String>(); 
   
    String k=c.getCurrentLoc();
    	    	  dataList.add(k);
    	    	  dataList.add(c.getCurrentLat());
    	    	  dataList.add(c.getCurrentlong());
    	    	  
    	    		
    	     
   
  
      request.setAttribute("data",dataList);
      RequestDispatcher dispatcher = request.getRequestDispatcher("BTS/page2.jsp");
      dispatcher.forward(request, response); 
}


}



but i got the default value of location .." undefined"... any suggestion??

Is This A Good Question/Topic? 0
  • +

Replies To: Servlet output changed

#2 Vikito   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-April 09

Re: Servlet output changed

Posted 31 March 2011 - 07:08 AM

View Postshaboury, on 22 March 2011 - 09:46 AM, said:

Weird thing happened to me
that s a function in a class where i can manipulate and get data from database and i tested it in a main and i got the right data
 
public String getCurrentLoc()
	{
		
		String location= "Undefined";
		try{
			System.out.println("Done");
			Statement stmt=con.createStatement();
			ResultSet rs=stmt.executeQuery("Select Location from  sheet1  Where Status= 'Current'");
			if(rs.next())
				{
				
				
					location=rs.getString(1);
					
					
				}
			
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		
	return location;	
	
	}


and i created an object of this class in a servlet
package BusTrackingSystem;
import java.io.*;
import java.util.*;
import java.sql.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.swing.text.html.HTMLdocument.Iterator;

public class getLoc extends HttpServlet{
	

  private ServletConfig config;
  DButils c= new DButils();
  public void init(ServletConfig config)
    throws ServletException{
     this.config=config;
     }
 
public void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException,IOException {
    
    PrintWriter out = response.getWriter();
    
    ArrayList<String> dataList = new ArrayList<String>(); 
   
    String k=c.getCurrentLoc();
    	    	  dataList.add(k);
    	    	  dataList.add(c.getCurrentLat());
    	    	  dataList.add(c.getCurrentlong());
    	    	  
    	    		
    	     
   
  
      request.setAttribute("data",dataList);
      RequestDispatcher dispatcher = request.getRequestDispatcher("BTS/page2.jsp");
      dispatcher.forward(request, response); 
}


}



but i got the default value of location .." undefined"... any suggestion??



The only reason i see is that your resultset is empty. That's why it skips the code in the if block. Try to check why the resultset would be empty. Also you can put "System.out.println("if block")" code in the if block to see if it gets in this block but i think it's pretty obvious that it doesn't since you get the default string.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1