In my application I have to retrieve names from database EMPLOYEE and display on my myPage.jsp page as a list when the page loads.

But the problem is that I fail to display so .

myPage.jsp

<html>
<body>
<form method="POST" action="/MyServlet">
<p>&nbsp;</p>
<p>&nbsp; </p>
<table width="13%" align="center" >
  <tr>
    <th scope="col">Names</th>
  </tr>
  <tr>
    <td height="356">&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>

MyServlet.java

package com.gbl;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


 public class SampleServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {

	 
	 DBInterFace data=new DBInterFace();
	 RequestDispatcher dispacher =null;
	 public SampleServlet() {
		super();
	}   	
	

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{			
			if(data.verify()!=null)
			{
				System.out.println("true");
				dispacher = getServletContext().getRequestDispatcher("myPage.jsp");
			}
			dispacher.forward(request,response);
   }  
	

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request,response);
	}   	  	    
}

DBInterface.java

package com.gbl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

//import com.datasource.DBConnection;

public class DBInterFace {

Connection con=getDBConnection();
public ArrayList verify(){
	 
	    ArrayList name_list=new ArrayList();
	     String query="select ENAME from EMPLOYEE" ;
	     System.out.println(query);
	     ResultSet rs=null;		       		        
	     java.sql.Statement st = null;
	     try {
	    	   st = con.createStatement();
	    	   rs=st.executeQuery(query);
	    	   while(rs.next()) 
	    	     {
				        String empName = rs.getString("ENAME");
	    	 	        out.print(empName);
	    		 }	    		 			   
			   rs.close();
			   			
		    }  
		  catch (SQLException e) 
		  {
			   e.printStackTrace();
		  }	           	
	return name_list;
}

The program seems a bit off. No need to implement Servlet; you are anyways extending HttpServlet. The DBInterface and RequestDispatcher members of your servlet class will be shared among all requests since each HTTP request spawns off a new thread which invokes the doXXX method [based on the request type]; if you need `request' scoped data, do all the processing and initialization in the doXXX methods.

Read the sticky at the top of this forum and the JEE 5 documentation to get started rather than experimenting with your concepts still in nascent stages.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.