简体   繁体   中英

Passing data queried from database in one servlet, to another Java Servlet

I am completing a project with some other class mates and we are stuck. In our Log In servlet, we are connecting to the database and retrieving the data matching the email & password criteria. The servlet adds the result row of information to a new user object , then placing the user in an Array. (The Array was created to try and pass all of the data at once), we have also tried only passing one attribute. Once the data is passed to the profile we need it to display next to specific labels. Currently we have not yet displayed the profile.html in the profile servlet, as we are just trying to verify that we can pass the variables first.

We need the information to pass like so : Database ---> LogInServlet-->ProfileServlet

Thanks!

LogInServlet

@WebServlet("/LogInServlet" )
public class LogInServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    List<User> users = new ArrayList<User>();

    public LogInServlet() {
        super();
    }
   
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
    
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        PrintWriter writer = response.getWriter();
        

        String selectQuery="SELECT * FROM `user` WHERE email = ? and password = ?" ;
        
        Connection connection =DBConnection.getConnectionToDatabase();
        java.sql.PreparedStatement statement;   
            try {
                
                statement = connection.prepareStatement(selectQuery);
                statement.setString(1,email);
                statement.setString(2,password);
                
                
                ResultSet set = statement.executeQuery();
                
                if(set.next()) {
                    
                    User user = new User();
                    user.setEmail(set.getString("email"));
                    user.setPassword(set.getString("password"));
                    user.setFirstName(set.getString("firstName"));
                    user.setLastName(set.getString("lastName"));
                    user.setDateOfBirth(set.getString("DOB"));
                    
                    users.add(user);
                    // THIS CODE TO SEND TO NEXT SERVLET
                    //request.setAttribute("email", set.getString(email));
                   // RequestDispatcher rd = request.getRequestDispatcher("/ProfileServlet");
                    //rd.forward(request, response);
                    
                    //FOLLOWING CODE IS TEST CODE TO PROFILE HTML
                   response.sendRedirect("profile.html");
                   
                } else {
                    String errorMessage ="<html>\r\n"
                            + "<head>\r\n"
                            + "<meta charset=\"ISO-8859-1\">\r\n"
                            + "<link rel=\"stylesheet\" type=\"text/css\" href=\"style2.css\">\r\n"
                            + "<title>Log In Error</title>\r\n"
                            + "</head>\r\n"
                            + "<body>\r\n"
                            + "<div class=\"accountMessage\">\r\n"
                            + "<h1 class=\"message\">Log in unsuccessful, please try again.</h1>\r\n"
                            + "<button class=\"messageLogIn\" onclick=\"history.back()\">Log In</button>\r\n"
                            + "</div>\r\n"
                            + "</body>\r\n"
                            + "</html>";
                    
                    writer.write(errorMessage);
                }
                set.close();
                statement.close();
                
                    
                
            } catch (SQLException e) {
                String errorMessage ="<html>\r\n"
                        + "<head>\r\n"
                        + "<meta charset=\"ISO-8859-1\">\r\n"
                        + "<link rel=\"stylesheet\" type=\"text/css\" href=\"style2.css\">\r\n"
                        + "<title>Log In Error</title>\r\n"
                        + "</head>\r\n"
                        + "<body>\r\n"
                        + "<div class=\"accountMessage\">\r\n"
                        + "<h1 class=\"message\">Log in unsuccessful, please try again.</h1>\r\n"
                        + "<button class=\"messageLogIn\" onclick=\"history.back()\">Log In</button>\r\n"
                        + "</div>\r\n"
                        + "</body>\r\n"
                        + "</html>";
                
                writer.write(errorMessage);
                e.printStackTrace();
            }
            
                
        
    }
   }

Profile Servlet

//PROFILE PAGE WOULD BE DISPLAY HERE WITH USER DATA QUERYED FROM LOG IN PAGE
@WebServlet("/ProfileServlet")
public class ProfileServlet extends HttpServlet {
     /**
     * 
     */
    private static final long serialVersionUID = -6589250865438405024L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            PrintWriter writer = response.getWriter();
           //List<User> users = (ArrayList<User>)request.getAttribute("myList");
            
           String email =(String)request.getAttribute("email");
           
            
            
        writer.write("<html><body>"+email+"</body></html>");
        }
}

Profile HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Profile Page</title>
<link rel="stylesheet" type="text/css" href="style3.css">
</head>
<body>

<img src="images/CanadaLogo.jpg" width="230px" height="100px">

<h3 class="profileHeading">Profile Page</h3>

<br>
<div class="navBarCon">
<!--Comments between divs remove white spaces-->
<div class="navItem"><a class="linkA" href="">Home</a></div><!--
  --><div class="navItem"><a class="linkA" href="">Verify Identity</a></div><!--
--><div class="navItem"><a class="linkA" href="">Voting Portal</a></div><!--
--><div class="navItem"><a class="linkA" href="">Help</a></div><!--
--><div class="navItem"><a class="linkA" href="LogIn.html">Log Out</a></div><!--

--></div>

<div class="profileCon">
<img src="images/profileIcon.png" width="150px" height="150px">
<h3 class="pLabel">Name: </h3> <!-- NAME DISPLAYED HERE -->
<h3 class="pLabel">Date of birth: </h3> <!-- DOB DISPLAYED HERE -->
<h3 class="pLabel">Email: </h3> <!-- EMAIL DISPLAYED HERE -->


</div>

</body>
</html>

If the user passes authentication, your LoginServlet can forward the request to the ProfileServlet, otherwise return an error page response (or forward to another Servlet that provides the error response).

RequestDispatcher dispatcher = getServletContext()
      .getRequestDispatcher("/ProfileServlet");
dispatcher.forward(request, response);

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