Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Unable to retrieve values from mySQL using JSP MVC application

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    19
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Unable to retrieve values from mySQL using JSP MVC application

    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


  2. #2
    Junior Member
    Join Date
    Aug 2013
    Posts
    19
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default Re: Unable to retrieve values from mySQL using JSP MVC application

    OK no response but I solved it myself. The solution is quite simple. The 'user' object passed in the application should have consistent naming. For example the 'user' object in the DAO. Instead of using:

     
    User u = new User();

    Use:

     
    User user = new User();

    Using the first example the user object has different names between the DAO, Controller servlet and jsp (View). Modifying the naming in this way means the object can be passed throughout the whole MVC application.

  3. The Following User Says Thank You to jaltaie For This Useful Post:

    GregBrannon (August 31st, 2013)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Unable to retrieve values from mySQL using JSP MVC application

    Thanks for taking the time to post the fix. This may be helpful to someone else, if the search engine is good enough for them to find it.

Similar Threads

  1. Recurssion programming to retrieve inner values of objects
    By balajigoud in forum Member Introductions
    Replies: 1
    Last Post: June 22nd, 2013, 04:17 PM
  2. retrieve data from database using Hashmap (multiple values in one key)
    By ashin12 in forum Collections and Generics
    Replies: 2
    Last Post: April 4th, 2012, 05:22 AM
  3. Replies: 1
    Last Post: February 8th, 2012, 12:08 AM
  4. Replies: 6
    Last Post: September 5th, 2011, 10:02 AM
  5. unable to get values for modification
    By javaking in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: September 17th, 2010, 05:27 AM

Tags for this Thread