简体   繁体   中英

Java - Servlet, Carry login information throughout project (session bean)

I have a login servlet, it works fine. But I would like for multiple servlets to be able to access the userID, valid, & facultyCB values to do different functions. I know the best way is with a session, can some1 please provide an example of writing, and calling from a session using my Login Servlet and ViewRegisteredCoursesServlet. If an object(bean) is stored with:

session.setAttribute("currentSessionUser", user);

How can you get different values from this in a servlet? EX. userId & facultyCB.

HttpSession session = request.getSession();
session.getAttribute("currentSessionUser");

Also, from what I understand the session needs to be forwarded (something thats not an option as depending on which nav bar is clicked a diff servlet is called). Correct?

LoginServlet:

    LoginBean user = new LoginBean();

    try {
        String checkBox = request.getParameter("facultyCB");

        user.setID(request.getParameter("id"));
        user.setPassword(request.getParameter("password"));

        if (checkBox != null) {
            user.setFacultyCB(true);
        } else {
            user.setFacultyCB(false);
        }

        user = LoginDAO.login(user);

        if (user.isValid()) {
            HttpSession session = request.getSession(true);
            if (checkBox != null) {
                user.setFacultyCB(true);
            } else {
                user.setFacultyCB(false);
            }

            session.setAttribute("currentSessionUser", user);
            response.sendRedirect("success_login.jsp"); //logged-in page

        } else {
            response.sendRedirect("invalid_login.jsp"); //error page 
        }

ViewRegisteredCoursesServlet:

String userID = "100000001";

    //Created code to determine term based on course dates
    String year = "2012";
    String term = "FALL";

    try {

        List<RegisteredCoursesBean> registeredCoursesArray = ViewRegisteredCoursesDAO.viewRegisteredCourses(userID, year, term);

        request.setAttribute("registeredCoursesBean", registeredCoursesArray);

        request.getRequestDispatcher("registered_courses.jsp").forward(request, response);

    } catch (Throwable theException) {
        System.out.println(theException);
    }

LoginBean:

private String id;
private String password;
private boolean facultyCB;
public boolean valid;

Thanks so much in advance!

if you want to Store a bean in and access it in other servlets. the best way you can do is to store that bean inside the session object.

HttpSession session = request.getSession();
session.setAttribute("BEANNAMEHERE", yourBeanObjectHere);

Accessing it in other servlets

HttpSession session = request.getSession();
YourBeanObject myBean =  (YourBeanObjectClass) session.getAttribute("BEANNAMEHERE");

SIDE NOTE I don't think it is a good idea to store the users Login information inside the session, perhaps just store he's ID and use that ID to retrieve his data from the database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM