【问题标题】:passing value to arraylist将值传递给arraylist
【发布时间】:2014-03-09 15:46:27
【问题描述】:

这是我的用户验证页面。 检查用户密码和 ID 后,我想在 ArrayList 中传递用户名。 之后,我想在主页中显示名称,但是, 我在我的主页中得到空值。 如何在ArrayList中添加用户名?

// DwB: I love sql injection attacks - this is bad.
rs=st.executeQuery("select * from registration where U_ID='"+
    U_ID+"' and Pass='"+Pass+"' ");
while(rs.next()){
    na1=rs.getString("U_ID");
    pw=rs.getString("Pass");
    Name=rs.getString("U_N");}

    if((U_ID.equals(na1))&&(Pass.equals(pw))){
        HttpSession session=request.getSession();
        //List<String> user= new ArrayList<>();
        //String id=(String)session.getAttribute("ID"); 

        //session.setAttribute("loggedIn",Name);

        String username = request.getParameter("Name");
        List<String> users = (List<String>)session.getAttribute("users");

        if(null == users) {
            users = new ArrayList<String>();
        }
        users.add(username);
        session.setAttribute("users", users);
        //response.sendRedirect("Welcome.jsp");

        request.setAttribute("U_ID",na1);
        request.getRequestDispatcher("/Welcome.jsp").forward(request, response);

【问题讨论】:

  • 你也可以粘贴你的jsp代码吗?
  • 还有你的堆栈跟踪
  • 1) 请坚持Java naming conventions,他们在那里是有原因的。此代码难以辨认。 2) 请阅读SQL Injection始终使用PreparedStatement。在 SQL 查询中看到String 连接让我(和许多其他人)很痛苦。

标签: java jsp arraylist


【解决方案1】:

不确定这是否有效,但这里有一些小的变化:

版本 1

    rs = st.executeQuery("select * from registration where U_ID='" + uId + "' and Pass='" + pass + "' ");  // changed the variables to lower case

    while (rs.next()) {
        na1 = rs.getString("U_ID");
        pw = rs.getString("Pass");
        name = rs.getString("U_N");
    }

    if ((uid.equalsIgnoreCase(na1)) && (pass.equalsIgnoreCase(pw))) {  // always compare two strings with string.equalsIgnoreCase(otherString);
        // ... do stuff with your list
    }

说明:在 Java 中,您的变量应该以小写字母开头,并且当您比较两个字符串时,然后将它们与方法 string1.euqalsIgnoreCase(string2); 进行比较。此方法将逐个比较字符串的每个字符。 string1.equals(string2) 方法只比较对象引用,不比较内容。

第 2 版

    rs = st.executeQuery("select U_N, COUNT(*) from registration where U_ID='" + uId + "' and Pass='" + pass + "' ");  // count(*) is enough, you don't need all the data from the row + changed the variables to lower case
    rs.next();
    if(rs.getInt(1)>0){ // the username/password kombination exists at least 1 times
       // .. do stuff with your list               
        name = rs.getString("U_N"); 
    }

说明:计算数据库中用户 id 和密码与您的搜索匹配的行就足够了。如果结果集的行数超过 0 行,则您的用户已登录

清单中的内容

    HttpSession session = request.getSession();

    //session.setAttribute("loggedIn",Name);
    String username = request.getParameter("Name");
    ArrayList<String> users = (ArrayList<String>) session.getAttribute("users");  // use ArrayList directly

    if (users == null) { 
        users = new ArrayList<>();  // diamond operator: the type is already given in the declaration
    }
    users.add(username);
    session.setAttribute("users", users);
    //response.sendRedirect("Welcome.jsp");

    request.setAttribute("uId", uId);
    request.getRequestDispatcher("/Welcome.jsp").forward(request, response);
    if (!session.containsKey(arrayListID)) {
    // Place the number the user entered into the session
        session.put(arrayListID, numbersEntered);
    } else {
        ArrayList<Integer> list = (ArrayList<Integer>) session.get(arrayListID);
        list.add(1 /* what you want */);
    // Retrieve session data
    }

解释:这并没有改变任何东西,但我只是看到在 if-clause(null==users) 中,您在转换为 List 之前实例化了 ArrayList。虽然这是完全允许的,但为什么不直接转换为 ArrayList 呢?

【讨论】:

    【解决方案2】:

    正如所写,除了 SQL 注入攻击漏洞的荣耀之外,当在数据库中找不到用户 ID 和密码时,保证您对 ArrayList 没有任何附加值。

    此代码if((U_ID.equals(na1))&amp;&amp;(Pass.equals(pw))) 是无价值的,因为除非该 if 陈述已经为真,否则您永远不会得到该 if 语句。您的查询只会找到与用户 ID 和密码匹配的行。

    您的问题似乎与区分大小写有关。 考虑更新数据库,使 U_ID 列全部大写(或全部小写) 然后适当调整用户输入的用户 ID 的大小写(全大写或全小写)。

    密码区分大小写似乎很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-11
      • 2017-01-25
      • 2014-05-02
      • 2017-07-28
      • 2015-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多