查看登录用户拥有的合同

我想在表中显示属于我的应用程序中用户登录名的合同,为此,我在身份验证servlet中创建了一个Map,以在此处将我登录的用户的ID和登录名存储在我的代码中:

   @Override
    protected void doPost(HttpServletRequest request,HttpServletResponse response)
            throws ServletException,IOException {
        String login1=request.getParameter("login1");
       String password1=request.getParameter("password1");
       String destination="/WEB-INF/acceuil_proprietaire.jsp";
       Connection c=Cnx.getcnx();

       try {

           PreparedStatement st=c.prepareStatement("select * from users where login=? and password=?");
           st.setString(1,login1);
           st.setString(2,password1);
           ResultSet res=st.executeQuery();
           if(res.next()){
                Map<String,String> user = new HashMap<String,String>();
                user.put("id",String.valueOf( res.getInt(1) ) );
                user.put("login",String.valueOf( res.getString(8) ) );
                request.getSession().setattribute("currentUser",user);
                System.out.println("connecter avec succes");
                if(res.getString("role").trim() == "proprietaire"){
                    destination="/WEB-INF/acceuil_proprietaire.jsp";

                }
                System.out.println("hello "+res.getString("role"));
                if(res.getString("role").equals("locataire")){
                    destination="/WEB-INF/acceuil_locataire.jsp";

                }

                if(res.getString("role").equals("admin")){
                    destination="/WEB-INF/acceuil_administrateur.jsp";
                }
           }
            else {
                request.setattribute("msg1","Login ou mot de passe incorrecte !");
                destination="/WEB-INF/Page_de_cnx.jsp";
            }  


    }   catch (SQLException ex) {
            Logger.getLogger(Authentification.class.getName()).log(Level.SEVERE,null,ex);
        }


       getServletContext().getRequestDispatcher(destination).forward(request,response);
    }
}

然后,我进入jsp并创建了一个Map,该Map检索了这样放置在身份验证servlet中的Map:

地图用户=(地图)session.getattribute(“ currentUser”);

在我提出请求后,我给了他一个条件,以向我显示已登录用户的合同,这是我的代码,可以更好地理解:

    <%
Connection c=Cnx.getcnx();
Statement st=c.createStatement();
Map<String,String> user = (Map<String,String>) session.getattribute("currentUser");
ResultSet re=st.executeQuery("select null,c.cin_locataire,c.cin_proprietaire,c.date_debut,c.date_fin,c.type_contrat,c.user_id from contrat c,users u where u.id="+ Integer.parseInt(user.get("id")) +"") ;

%>

您看到我满足条件的情况

where u.id="+ Integer.parseInt(user.get("id")) +""

但是我看到这行不通,他向我展示了我在数据库中拥有的所有合同,这是一个演示图像:

查看登录用户拥有的合同

此刻我与ID为6的用户建立了联系,因此他也不应向我显示ID为8的用户的合同。

我真的不知道错误来自哪里。

如果需要,这是我所有的jsp代码:

  <%@page import="controller.Cnx"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="java.util.Map"%>

<!DOCTYPE html>


<%
Connection c=Cnx.getcnx();
Statement st=c.createStatement();
Map<String,users u where u.id="+ Integer.parseInt(user.get("id")) +"") ;





%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="description" content="Sufee Admin - HTML5 Admin Template">
        <meta name="viewport" content="width=device-width,initial-scale=1">

        <link rel="apple-touch-icon" href="apple-icon.png">
        <link rel="shortcut icon" href="favicon.ico">

        <link rel="stylesheet" href="vendors/bootstrap/dist/css/bootstrap.min.css">
        <link rel="stylesheet" href="vendors/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="vendors/themify-icons/css/themify-icons.css">
        <link rel="stylesheet" href="vendors/flag-icon-css/css/flag-icon.min.css">
        <link rel="stylesheet" href="vendors/selectFX/css/cs-skin-elastic.css">
        <link rel="stylesheet" href="vendors/jqvmap/dist/jqvmap.min.css">


        <link rel="stylesheet" href="assets/css/style.css">
        <link rel="stylesheet" href="css/dataTables.bootstrap4.min.css">
        <link rel="stylesheet" href="css/style.css.map.css">
        <link href='https://fonts.googleapis.com/css?family=Open+Sans:400,600,700,800' rel='stylesheet' type='text/css'>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnqq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmvgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
        <script src="https://kit.fontawesome.com/e3fd0d5f24.js" crossorigin="anonymous"></script>

        <title>Contrat</title>
    </head>
    <body>
        <aside id="left-panel" class="left-panel">
        <nav class="navbar navbar-expand-sm navbar-default" style="flex-flow:column wrap;">

            <div class="navbar-header">

                <a class="navbar-brand" href="#">Gestion parkings</a>
                <a class="navbar-brand hidden" href="./"><img src="images/logo2.png" alt="Logo"></a>
            </div>

            <div id="main-menu" class="main-menu collapse navbar-collapse">
                <ul class="nav navbar-nav">


                    <li class="nav-item ">
                        <a href="Gestion_des_locaux" class="nav-link"  aria-haspopup="true" aria-expanded="false"> <i class="fas fa-arrow-alt-circle-right" style="margin-right: 20px;" selected></i>Gestion locaux</a>

                    </li>
                    <li class="nav-item ">
                        <a href="Place_locaux" class="nav-link"  aria-haspopup="true" aria-expanded="false"> <i class="fas fa-arrow-alt-circle-right" style="margin-right: 20px;"></i>Places</a>

                    </li>
                    <li class="nav-item ">
                        <a href="#" class="nav-link"  aria-haspopup="true" aria-expanded="false"> <i class="fas fa-arrow-alt-circle-right" style="margin-right: 20px;"></i>Contrats</a>

                    </li>



                </ul>
            </div>
        </nav>
    </aside>

    <nav class="navbar" style="height:70px;" id='nav-proprietaire'>
        <ul class="nav navbar-nav">
            <li class="nav-item ">
                        <a href="Page_de_cnx" class="nav-link"  aria-haspopup="true" aria-expanded="false" style="padding-left: 1430px; text-transform: uppercase"> <i class="fas fa-exclamation-circle" style="margin-right: 20px;"></i>Déconnection</a>

                    </li>
        </ul>
    </nav>
        <label style="text-align: center; text-transform: uppercase; font-size: 30px; color:#0e9ed8;">All contrats :</label>
        <table class="table table-striped table-bordered" style="margin-top: 20px;" id="table1">
  <thead>
    <tr class="table-active">
      <th scope="col">Cin_locataire</th>
      <th scope="col">Cin_proprietaire</th>
      <th scope="col">Date debut</th>
      <th scope="col">Date fin</th>
      <th scope="col">Type de contrat</th>
      <th scope="col">User id</th>


    </tr>
  </thead>
  <tbody>


     <%while(re.next()){%> 
     <tr>

            <th scope="row"><%=re.getObject(2)%></th>
            <td><%=re.getObject(3)%></td>
            <td><%=re.getObject(4)%></td>
            <td>><%=re.getObject(5)%></td>
            <td><%=re.getObject(6)%></td>
            <td><%=re.getObject(7)%></td>
     </tr>


      <%}%>


  </tbody>

</table>





        <script src="vendors/jquery/dist/jquery.min.js"></script>
    <script src="vendors/popper.js/dist/umd/popper.min.js"></script>
    <script src="vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <script src="assets/js/main.js"></script>
    <script src="js/dataTables.bootstrap4.min.js"></script>
    <script src="js/jquery-3.3.1.js"></script>
    <script src="js/jquery-3.3.1.min.js"></script>
    <script src="js/jquery.dataTables.min.js"></script>
    <script src="vendors/chart.js/dist/Chart.bundle.min.js"></script>
    <script src="assets/js/dashboard.js"></script>
    <script src="assets/js/widgets.js"></script>
    <script src="vendors/jqvmap/dist/jquery.vmap.min.js"></script>
    <script src="vendors/jqvmap/examples/js/jquery.vmap.sampledata.js"></script>
    <script src="vendors/jqvmap/dist/maps/jquery.vmap.world.js"></script>    
    </body>
</html>

如果您能帮助我解决我的问题,并在此先感谢您的帮助,我将不胜感激!

qazwsxedcr123 回答:查看登录用户拥有的合同

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3169904.html

大家都在问