简体   繁体   English

将从一个 servlet 中的数据库查询的数据传递到另一个 Java Servlet

[英]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.在我们的登录 servlet 中,我们连接到数据库并检索与电子邮件和密码条件匹配的数据。 The servlet adds the result row of information to a new user object , then placing the user in an Array. servlet 将信息的结果行添加到新的用户对象中,然后将用户放入一个数组中。 (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.目前我们还没有在配置文件 servlet 中显示 profile.html,因为我们只是在尝试验证我们可以先传递变量。

We need the information to pass like so : Database ---> LogInServlet-->ProfileServlet我们需要这样传递信息:Database ---> LogInServlet-->ProfileServlet

Thanks!谢谢!

LogInServlet登录Servlet

@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配置文件 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个人资料 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).如果用户通过身份验证,您的 LoginServlet 可以将请求转发给 ProfileServlet,否则返回错误页面响应(或转发给另一个提供错误响应的 Servlet)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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