Further below is code for a class named User
.
I am getting records of a table and storing them in the User
class object which is added in the ArrayList
class object. At last, all the records of the table will be stored in the ArrayList
class object (collection). Finally, we are storing the ArrayList
object in the ServletConext
object as an attribute so that we can get it in the servlet and use it.
But when I execute my datab
listener class, I face the "404 - Not Found" issue shown in the below image. I don't know where I am going wrong.
I am using Glassfish Server and I have created a web.xml file making entries for all listeners, servlets, and servlet mappings.
Bean ---User class
public class User implements java.io.Serializable {
String name,password;int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Listener class
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.sql.*;
import java.util.ArrayList;
import javax.servlet.annotation.WebServlet;
@WebServlet(urlPatterns={"/datab"})
public class datab implements ServletContextListener{
public void contextInitialized(ServletContextEvent e) {
System.out.println("HI");
ArrayList list=new ArrayList();
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin:@12.16.19.10:1521:ROW0", "global_urs_prd", "catcat1");
PreparedStatement ps=con.prepareStatement("select mc_name,folder_name from ogl_table_status where status <>0");
ResultSet rs=ps.executeQuery();
while(rs.next()){
try{ User u=new User();
//u.setId(rs.getInt(1));
u.setName(rs.getString(1));
u.setPassword(rs.getString(2));
list.add(u);}catch(Exception e1){System.out.print(e1);}
}
con.close();
}catch(Exception ex){System.out.print(ex);}
//storing the ArrayList object in ServletContext
ServletContext context=e.getServletContext();
context.setAttribute("data",list);
}
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("project undeployed...");
}
}