I'm testing a JSP MVC application on Netbeans 731. This is an admin application which simply queries a mySql DB and returns a list of users. The application starts with index.jsp where the user clicks the 'display' hyperlink. This then calls a displayUsers GET action which uses the DisplayUsersServlet. The DBUser class is used as a DAO that talks to the DB.
The application compiles and runs without any problems. I even tested the Connection pooling and manually queried the DB to make sure the values are there. But when I run the application I just see the columns displayed without the values where the resulting jsp page (users.jsp) is supposed to show a list of users. The list is retrieved using an ArrayList object in the DAO UserDB.java). The DAO is called by the controller servlet (DisplayUsersServlet.java). The below log4j output suggests that the DAO is talking to the database so I'm not sure what I'm doing wrong.
The problem I can see is that the Http request produces a NULL QueryString. So that could be the problem or something is going wrong in the users.jsp which does not display the resultset?
If I can't get an answer to why the final jsp doesn't display the DB values, it would at least be good to know how to use the log4j inside the users.jsp so I could see if the expression language of the jsp is the cause.
Would really appreciate any advice. Thank you!
My code is:
index.jsp
INDEX.JSP <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Murach's Java Servlets and JSP</title> </head> <body> <h1>User Administration Application</h1> <a href="displayUsers" method="get">Display Users</a> </body> </html>
package data; import java.sql.*; import java.util.ArrayList; import business.User; public class UserDB { public static int insert(User user) { ConnectionPool pool = ConnectionPool.getInstance(); Connection connection = pool.getConnection(); PreparedStatement ps = null; String query = "INSERT INTO User (FirstName, LastName, EmailAddress) " + "VALUES (?, ?, ?)"; try { ps = connection.prepareStatement(query); ps.setString(1, user.getFirstName()); ps.setString(2, user.getLastName()); ps.setString(3, user.getEmailAddress()); return ps.executeUpdate(); } catch(SQLException e) { e.printStackTrace(); return 0; } finally { DBUtil.closePreparedStatement(ps); pool.freeConnection(connection); } } public static int update(User user) { ConnectionPool pool = ConnectionPool.getInstance(); Connection connection = pool.getConnection(); PreparedStatement ps = null; } public static ArrayList<User> selectUsers() { ConnectionPool pool = ConnectionPool.getInstance(); Connection connection = pool.getConnection(); PreparedStatement ps = null; ResultSet rs = null; String query = "SELECT * FROM User "; try { ps = connection.prepareStatement(query); rs=ps.executeQuery(); // User users = null; ArrayList<User> users = new ArrayList<User>(); while(rs.next()) { User u = new User(); u.setFirstName(rs.getString("FirstName")); u.setLastName(rs.getString("LastName")); u.setEmailAddress(rs.getString("EmailAddress")); users.add(u); } return users; } catch(SQLException e) { e.printStackTrace(); return null; } finally{ DBUtil.closeResultSet(rs); DBUtil.closePreparedStatement(ps); pool.freeConnection(connection); } } }
package user; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.ArrayList; import business.User; import data.UserDB; public class DisplayUsersServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO: get an ArrayList of User objects from the database //UserDB.selectUsers(); HttpSession session = request.getSession(); ArrayList<User> selectUsers = UserDB.selectUsers(); session.setAttribute("selectUsers", selectUsers); String url = "/users.jsp"; RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url); dispatcher.forward(request, response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
users.jsp
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Murach's Java Servlets and JSP</title> </head> <body> <h1>Users List</h1> <table cellpadding="5" border=1> <tr valign="bottom"> <th>First Name</th> <th>Last Name</th> <th>Email Address</th> </tr> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:forEach var="user" items="${users}"> <tr valign="top"> <td><p>${user.firstName}</td> <td><p>${user.lastName}</td> <td><p>${user.emailAddress}</td> <td><a href="displayUser?emailAddress=${user.emailAddress}">Update</a></td> <td><a href="deleteUser?emailAddress=${user.emailAddress}">Delete</a></td> </tr> </c:forEach> </table> </body> </html>
log4j output:
Aug 29, 2013 3:45:09 PM org.apache.catalina.core.StandardContext reload INFO: Reloading Context with name [/ch14userAdmin] is completed 2013-08-29 15:45:14 INFO ch14userAdmin:191 - DAO query result: John 2013-08-29 15:45:14 INFO ch14userAdmin:192 - DAO query result: Smith 2013-08-29 15:45:14 INFO ch14userAdmin:193 - DAO query result: jsmith@gmail.com 2013-08-29 15:45:14 INFO ch14userAdmin:197 - The arrray object added is: business.User@7009665c 2013-08-29 15:45:14 INFO ch14userAdmin:198 - DAO RETURNS: [business.User@7009665c] 2013-08-29 15:45:14 INFO ch14userAdmin:191 - DAO query result: Andrea 2013-08-29 15:45:14 INFO ch14userAdmin:192 - DAO query result: Steelman 2013-08-29 15:45:14 INFO ch14userAdmin:193 - DAO query result: andi@murach.com 2013-08-29 15:45:14 INFO ch14userAdmin:197 - The arrray object added is: business.User@791f5b7a 2013-08-29 15:45:14 INFO ch14userAdmin:198 - DAO RETURNS: [business.User@7009665c, business.User@791f5b7a] 2013-08-29 15:45:14 INFO ch14userAdmin:191 - DAO query result: Joel 2013-08-29 15:45:14 INFO ch14userAdmin:192 - DAO query result: Murach 2013-08-29 15:45:14 INFO ch14userAdmin:193 - DAO query result: joelmurach@yahoo.com 2013-08-29 15:45:14 INFO ch14userAdmin:197 - The arrray object added is: business.User@79aee4b5 2013-08-29 15:45:14 INFO ch14userAdmin:198 - DAO RETURNS: [business.User@7009665c, business.User@791f5b7a, business.User@79aee4b5] 2013-08-29 15:45:14 DEBUG ch14userAdmin:23 - Result of the arraylist from DAO to Diplay servlet is:[business.User@7009665c, business.User@791f5b7a, business.User@79aee4b5] 2013-08-29 15:45:14 DEBUG ch14userAdmin:30 - The session set is: org.apache.catalina.session.StandardSessionFacade@e55c068 2013-08-29 15:45:14 DEBUG ch14userAdmin:34 - The Servlet forwards to /users.jsp 2013-08-29 15:45:14 DEBUG ch14userAdmin:36 - The HTTP request is from the display servlet is: uri: /ch14userAdmin/displayUsers method: GET QueryString: null Parameters: Headers: Name: accept Value: image/jpeg, image/gif, image/pjpeg, application/x-ms-application, application/xaml+xml, application/x-ms-xbap, */* Name: dnt Value: 1 Name: referer Value: http://localhost:8084/ch14userAdmin/ Name: accept-language Value: en-US Name: user-agent Value: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E) Name: accept-encoding Value: gzip, deflate Name: host Value: localhost:8084 Name: connection Value: Keep-Alive Name: cookie Value: JSESSIONID=2722B1786DCA7EE17C993DD65A4584AA 2013-08-29 15:45:14 DEBUG ch14userAdmin:37 - The HHTP response is: org.netbeans.modules.web.monitor.server.MonitorResponseWrapper@4f651ff