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 14 of 14

Thread: Using session atttributes and DAO class to delete mySql user records

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

    Default Using session atttributes and DAO class to delete mySql user records

    I'm trying to write a form that deletes users from a database using emailAddress as primary key. I'm stuck on how to set the user object. Do I create a new user object (User user = new User())? Do I pull in the user from the prior user list using the session attribute (User user = session.getAttribute("user))? Is there some other method for pulling in the user object and using the object to delete the user?

    My class is as follows. I've also include an extract of the error trace. Please let me know if I'm not being clear or you need more information.

    package user;
     
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
     
    import business.User;
    import data.UserDB;
    import org.apache.log4j.Logger;
     
    public class DeleteUserServlet extends HttpServlet {
     
        private static Logger logger = Logger.getLogger("ch14userAdmin");
     
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
            HttpSession session = request.getSession();
     
    // do I create the user object like this?
     
            **** User user = (User) session.getAttribute("user");*****
     
     
            logger.debug("Delete get user object returns: " + session.getAttribute("User"));
     
     
            String emailAddress = request.getParameter("emailAddress");
     
    // Or like this?
    ******        User user = (User) session.getAttribute("user");
     
    // Do I need to set the request parameter?
     
    *****   user.setEmailAddress(emailAddress);
     
            UserDB.delete(user);
            logger.debug("Delete method returns " + UserDB.delete(user));
     
            // TODO: add code that deletes the user that corresponds with the email address from the database
     
     
     
            String url = "/displayUsers";
            RequestDispatcher dispatcher =
                    getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);
        }
    }

     
    Sep 02, 2013 12:19:07 PM org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [DeleteUserServlet] in context with path [/ch14userAdmin] threw exception
    java.lang.NullPointerException
    	at user.DeleteUserServlet.doGet(DeleteUserServlet.java:27)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    .
    .
    .
    Last edited by jaltaie; September 3rd, 2013 at 12:53 PM. Reason: Add error trace


  2. #2
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Using session atttributes and DAO class to delete mySql user records

    I'm not sure I understand the question. Your code throws a NullPointerException, so what's on line 27 of the code you ran to produce this error? Do you ever set the session attribute? Does the UserDB.delete method only requre the email address of User to be set?

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

    Default Re: Using session atttributes and DAO class to delete mySql user records

    Sorry if I wasn't clearly. My question is how can I correctly pass the user object so that the UserDB.delete methods functions correctly?

    Line 27 is: user.setEmailAddress(emailAddress);

    I don't set the session attribute. I GET the session attribute instead. The UserDB.delete method is as follows:

    public static int delete(User user) {
            ConnectionPool pool = ConnectionPool.getInstance();
            Connection connection = pool.getConnection();
            PreparedStatement ps = null;
     
            String query = "DELETE FROM User "
                    + "WHERE EmailAddress = ?";
            try {
                ps = connection.prepareStatement(query);
                ps.setString(1, user.getEmailAddress());
     
                return ps.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
                return 0;
            } finally {
                DBUtil.closePreparedStatement(ps);
                pool.freeConnection(connection);
            }
        }

    Does the above make my question clearer? Thanks

  4. #4
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Using session atttributes and DAO class to delete mySql user records

    If you don't set the session attribute, then you can't get it. If you pass the email address using GET, then pass this value to delete the user - be it through a new User object, or by creating a method that deletes the user based upon a parameter String object representing the email address.

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

    Default Re: Using session atttributes and DAO class to delete mySql user records

    Are you saying I have to set and get the session attribute in the same servlet? What if I want to use the session attribute that was set in a prior servlet? For example:

     
    package user;
     
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
     
    import business.User;
    import data.UserDB;
    import java.util.ArrayList;
     
    public class DisplayUserServlet extends HttpServlet {
     
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
     
            String emailAddress = request.getParameter("emailAddress");
            User user = UserDB.selectUser(emailAddress);
     
            HttpSession session = request.getSession();
     
    //        session.setAttribute("user", user);
     
            String url = "/user.jsp";
            RequestDispatcher dispatcher =
                    getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);
        }
    }

  6. #6
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Using session atttributes and DAO class to delete mySql user records

    Are you saying I have to set and get the session attribute in the same servlet?
    A session attribute is meant to allow attributes to be shared during a session. If you set the "user" attribute in another servlet, say when a user logs in, then you should be able to get it in this servlet provided the user session is still active.

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

    Default Re: Using session atttributes and DAO class to delete mySql user records

    This is very useful to know but I still don't understand. What I'm creating here is actually an admin application. So the session attribute is set when the admin logs in and views a list of users which uses the DisplaytUserServlet mentioned above. If I want to use the session attribute from this display I can assume that the user session is still active since the UPDATE link (which calls the UserDeleteServlet action mentioned above) is on the list of users. Given this situation can I assume that the user session is still active from the list of users? Therefore, can I use the session attribute set by DisplayUserServlet?

    Thanks in advance!

  8. #8
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Using session atttributes and DAO class to delete mySql user records

    What I'm creating here is actually an admin application. So the session attribute is set when the admin logs in and views a list of users which uses the DisplaytUserServlet mentioned above.
    Ok, sounds good.

    If I want to use the session attribute from this display I can assume that the user session is still active since the UPDATE link (which calls the UserDeleteServlet action mentioned above) is on the list of users. Given this situation can I assume that the user session is still active from the list of users?
    Don't know what you mean here. Are you saying because the UserDeleteServlet gets the correct info, you wish to use this as validation that the session is still active? Why not rely on getting the session attribute directly?

    Therefore, can I use the session attribute set by DisplayUserServlet?
    A session attribute, if set previously, will still be available if the session is still active, so you should still be able to retrieve it. I'm still not quite sure I follow your question however. Perhaps post a workflow as to what you want, what you've tried, and what you got.

  9. The Following User Says Thank You to copeg For This Useful Post:

    jaltaie (September 4th, 2013)

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

    Default Re: Using session atttributes and DAO class to delete mySql user records

    Workflow looks something like this:

    UserDisplayServlet ---> user.jsp(lists a bunch of users) ---> 2 sets hyperlinks calling 2 separate actions for each user listed (update and delete)

    ---> CLICK DELETE ---> Calls DeleteUserServlet (which deletes based on the email address of the selected user in user.jsp

    Is the above a clear workflow?

    Don't know what you mean here. Are you saying because the UserDeleteServlet gets the correct info, you wish to use this as validation that the session is still active? Why not rely on getting the session attribute directly?
    I'm not sure what you mean by 'getting the session attribute directly'. Do you mean get the attribute from DeleteUserServlet, instead of relying on getting it from DisplayUserServlet?

    I'm asking if I HAVE to get the attribute from DeleteUserServlet to preserve the values stored in the session (since the email address value is used as a primary key to delete the selected user)?

    I think what I'm not understanding is if I SET the session attribute in the UserDeleteServlet will it clear the primary key (email address) selected in the user list since I'm setting a new session? If the answer is that I can set the attribute in the DeleteUserServlet, will this preserve the email address value for the selected user? If I set the attribute in the DeleteUserServlet does this mean that I also have to GET the attribute in the same servlet?

    Hope I'm making more sense. Thanks again

  11. #10
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Using session atttributes and DAO class to delete mySql user records

    UserDisplayServlet ---> user.jsp(lists a bunch of users) ---> 2 sets hyperlinks calling 2 separate actions for each user listed (update and delete)
    Based upon this, it sounds like you wish to delete a particular user, not the user currently logged in. Is that correct? If so the getAttribute will return the current logged in user (presuming that's how its set)...to delete a different user pass the email as a parameter to the delete servlet (as it looks like you have) and use this to delete

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

    Default Re: Using session atttributes and DAO class to delete mySql user records

    Correct I want to delete a particular user not the logged in user. The logged in user is the admin.

    to delete a different user pass the email as a parameter to the delete servlet (as it looks like you have) and use this to delete
    From the above do you mean that I don't even need to get or set the session attribute? Just pass the parameter then create a new user object and call the UserBD.delete?

  13. #12
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,099
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Using session atttributes and DAO class to delete mySql user records

    Quote Originally Posted by jaltaie View Post
    From the above do you mean that I don't even need to get or set the session attribute? Just pass the parameter then create a new user object and call the UserBD.delete?
    What did you try? You should somehow at least check that the user is admin to be sure the user doing the deletion has the appropriate permissions.

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

    Default Re: Using session atttributes and DAO class to delete mySql user records

    The user logged on does have admin permission. This is what I'm trying:

    package user;
     
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
     
    import business.User;
    import data.UserDB;
    import org.apache.log4j.Logger;
     
    public class DeleteUserServlet extends HttpServlet {
     
        private static Logger logger = Logger.getLogger("ch14userAdmin");
     
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
     
            HttpSession session = request.getSession();
     
            logger.debug("Delete get user object returns: " + session.getAttribute("User"));
     
            String emailAddress = request.getParameter("emailAddress");
            User user = new User();
                   session.setAttribute("user", user);
     
     
            UserDB.delete(user);
     
     
            logger.debug("Delete method returns " + UserDB.delete(user));
     
     
            String url = "/displayUsers";
            RequestDispatcher dispatcher =
                    getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);
        }
    }

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

    Default Re: Using session atttributes and DAO class to delete mySql user records

    Here's what I figured out so far:

    - The session attribute value for the list of users is called ('users"), which is set in the DisplayUsersServlet which returns an ArrayList of users (I tried using DisplayUsersServlet, it works and displays the list in the users.jsp)

    - The session attribute value for the update part is called DisplayUserServlet(singular user instead of users). This is rendered by user.jsp, which displays a form for the selected user

    This tells me that I should pass the users session attribute (from the list of users) in the DeleteUsersServlet. This is what I tried:

     
     
    package user;
     
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
     
    import business.User;
    import data.UserDB;
    import org.apache.log4j.Logger;
     
    public class DeleteUserServlet extends HttpServlet {
     
        protected void doGet(HttpServletRequest request,
                HttpServletResponse response)
                throws ServletException, IOException {
     
            HttpSession session = request.getSession();
     
     
    //       User user =  (User) session.getAttribute("users");  
     
            String emailAddress = request.getParameter("emailAddress");
     
            UserDB.delete(user);
     
     
             session.setAttribute("users", user);
            String url = "/displayUsers";
            RequestDispatcher dispatcher =
                    getServletContext().getRequestDispatcher(url);
            dispatcher.forward(request, response);
        }
    }
    The problem is I can't seem to cast an ArrayList to a User type object and I get the following error:

    java.lang.ClassCastException: java.util.ArrayList cannot be cast to business.User
    user.DeleteUserServlet.doGet(DeleteUserServlet.jav a:22)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet .java:728)

    Am I right in assuming that I need to pass the "users" session attribute in the DeleteUsersServlet? Any advice about how to deal with the ClassCastException?
    Thank you

Similar Threads

  1. Replies: 1
    Last Post: June 29th, 2012, 01:29 PM
  2. delete item from mysql database
    By some_one in forum AWT / Java Swing
    Replies: 3
    Last Post: April 9th, 2012, 10:32 AM
  3. Junit for DAO class
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: January 11th, 2012, 01:21 AM
  4. How can i get multiple records from mySQl database
    By mDennis10 in forum JDBC & Databases
    Replies: 2
    Last Post: September 2nd, 2011, 05:41 PM
  5. Track the user session list
    By kalees in forum Java Servlet
    Replies: 3
    Last Post: October 24th, 2009, 02:34 PM