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> </p>
<p> </p>
<table width="13%" align="center" >
<tr>
<th scope="col">Names</th>
</tr>
<tr>
<td height="356"> </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;
}