Hello list users,
Maybe this is not the best place to ask a general servlet doubt, but I'm hoping someone can point out the mistake I'm making. I'm attaching a servlet file (FetchEmployeeServlet.java), which for some strange reason, gives a Null-Pointer Exception when accessing a function. This tells me that the class is not instantiated or something, but I'm new to servlets, so I can't figure out why.
TIA, Vamsee.
-- Because joy is one's fuel - Ayn Rand
package com.vamsee.empdb;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import javax.sql.*;
import java.util.*;
import javax.naming.*;
public class FetchEmployeeServlet extends HttpServlet {
private Connection conn = null;
private ServletContext context;
private PreparedStatement pstmt = null;
public void init(ServletConfig config) throws ServletException {
super.init(config);
context = config.getServletContext();
try {
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:comp/env");
if (envContext == null)
throw new Exception("Panic: No Context!");
DataSource ds = (DataSource)envContext.lookup("jdbc/empdb");
if (ds != null) {
Connection conn = ds.getConnection();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String jsp;
String cmd = req.getParameter("cmd");
String idString = req.getParameter("id");
int id;
try {
id = Integer.parseInt(idString);
} catch (NumberFormatException e) {
id = 0;
}
if ("get".equals(cmd)) {
EmployeeBean bean = this.fetchEmployee(id);
req.setAttribute("employee", bean);
jsp = "/employee.jsp";
} else {
List list = this.fetchAll();
req.setAttribute("list", list);
jsp = "/list.jsp";
}
RequestDispatcher dispatcher;
dispatcher = context.getRequestDispatcher(jsp);
dispatcher.forward(req, res);
}
public EmployeeBean makeBean(ResultSet results)
throws SQLException {
EmployeeBean bean = new EmployeeBean(results.getInt("id"));
bean.setFirstName(results.getString("fname"));
bean.setLastName(results.getString("lname"));
bean.setEmail(results.getString("email"));
bean.setDepartment(results.getString("department"));
bean.setImage(results.getString("image"));
return bean;
}
public EmployeeBean fetchEmployee(int id) {
EmployeeBean bean = null;
try {
ResultSet results;
String sql = "select * from people_table where id = ?";
PreparedStatement pstmt = conn.prepareStatement(sql);
synchronized (pstmt) {
pstmt.clearParameters();
pstmt.setInt(1, id);
results = pstmt.executeQuery();
}
if (results.next())
bean = makeBean(results);
if (results != null)
results.close();
} catch (SQLException se) {
se.printStackTrace();
}
return bean;
}
public List fetchAll() {
List list = new ArrayList();
try {
ResultSet results;
Statement st = conn.createStatement();
System.out.println("Okay until create statement");
results = st.executeQuery("select * from people_table");
System.out.println("Okay until execute query");
while (results.next())
list.add(makeBean(results));
} catch (SQLException se) {
se.printStackTrace();
}
return list;
}
public void destroy() {
try {
if (conn != null)
conn.close();
} catch (SQLException e) { }
}
}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
